package com.example.task;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.entity.mysql.Productprocessresource;
import com.example.entity.mysql.Resource;
import com.example.entity.mysql.Resourcegroup;
import com.example.entity.vo.hzView.*;
import com.example.entity.yifei.*;
import com.example.enums.MouldStatusEnum;
import com.example.exception.CustomException;
import com.example.mapper.*;
import com.example.mapper.hzViewSync.HzViewSyncMapper;
import com.example.mapper.pc.ProductprocessresourceMapper;
import com.example.mapper.pc.ResourceMapper;
import com.example.mapper.pc.ResourcegroupMapper;
import com.example.mapper.pc.ScheduleMapper;
import com.example.service.*;
import com.example.service.pc.ProductprocessresourceService;
import com.example.service.pc.ResourceService;
import com.example.service.pc.ResourcegroupService;
import com.example.service.pc.WorkresourceService;
import com.example.untils.BeanUtils;
import com.example.untils.LanguageConversionUtils;
import com.example.untils.StringUtils;
import com.example.untils.TimeUtils;
import com.github.houbb.opencc4j.core.impl.ZhConvertBootstrap;
import com.github.houbb.opencc4j.support.segment.impl.Segments;
import com.github.houbb.opencc4j.util.ZhTwConverterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 *  宏竹VIEW特供版
 */
@Component
@Order(Integer.MAX_VALUE)
public class ResourceGroupSyncTask {
    @Autowired
    private ScheduleMapper scheduleMapper;
    @Autowired
    private ViewsynclogService viewsynclogService;
    @Autowired
    private WorkresourceService workresourceService;
    @Autowired
    private HzViewSyncMapper hzViewSyncMapper;
    @Autowired
    private ResourcegroupMapper resourcegroupMapper;
    @Autowired
    private ResourcegroupService resourcegroupService;
    @Autowired
    private ProductprocessresourceService productprocessresourceService;
    @Autowired
    private ProductprocessresourceMapper productprocessresourceMapper;
    @Autowired
    private TablesynclogService tablesynclogService;
    @Autowired
    private InvmbMapper invmbMapper;
    @Autowired
    private InvmbService invmbService;
    @Autowired
    private CmsmeMapper cmsmeMapper;
    @Autowired
    private CmsmvMapper cmsmvMapper;
    @Autowired
    private CmsmeService  cmsmeService;
    @Autowired
    private CmsmvService cmsmvService;
    @Autowired
    private CmsmcMapper cmsmcMapper;
    @Autowired
    private CmsmcService cmsmcService;
    @Autowired
    private BommfMapper bommfMapper;
    @Autowired
    private BommfService bommfService;
    @Autowired
    private CmsmdService cmsmdService;
    @Autowired
    private CmsmdMapper cmsmdMapper;
    @Autowired
    private PurmaService purmaService;
    @Autowired
    private PurmaMapper purmaMapper;
    @Autowired
    private BommeMapper bommeMapper;
    @Autowired
    private BommeService bommeService;
    @Autowired
    private ResourceMapper resourceMapper;
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private CoptdMapper coptdMapper;
    @Autowired
    private CoptdService coptdService;
    @Autowired
    private PurcdMapper purcdMapper;
    @Autowired
    private PurcdService purcdService;
    @Autowired
    private PurtdMapper purtdMapper;
    @Autowired
    private PurtdService purtdService;
    @Autowired
    private MoctaMapper moctaMapper;
    @Autowired
    private MoctaService moctaService;
    @Autowired
    private SfctaMapper sfctaMapper;
    @Autowired
    private SfctaService sfctaService;
    @Autowired
    private MoctbService moctbService;
    @Autowired
    private MoctbMapper moctbMapper;
    @Autowired
    private MoctyService moctyService;
    @Autowired
    private MoctyMapper moctyMapper;
    @Autowired
    private Mocta2Mapper mocta2Mapper;
    @Autowired
    private Mocta2Service mocta2Service;
    @Autowired
    private Moctb2Mapper moctb2Mapper;
    @Autowired
    private Moctb2Service moctb2Service;
    @Autowired
    private MocteMapper mocteMapper;
    @Autowired
    private MocteService mocteService;
    @Autowired
    private MoctgMapper moctgMapper;
    @Autowired
    private MoctgService moctgService;
    @Autowired
    private CmsmwMapper cmsmwMapper;
    @Autowired
    private CmsmwService cmsmwService;
    @Autowired
    private CopmaMapper copmaMapper;
    @Autowired
    private CopmaService copmaService;

    @Value("${spring.profiles.active}")
    private String active;

    @Scheduled(cron = "0 0/5 * * * ?")
    @DSTransactional
    public void invmb_bommfViewSync() {
        System.out.println("active = " + active);
        if(!"hz".equals(active)) {
            return;
        }
        System.out.println("invmbViewSync....." + TimeUtils.getFullNowTime());
        Tablesynclog invmbLog = tablesynclogService.getOne(new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "INVMB"));
        List<HzInvmbView> viewList = null;
        if(invmbLog == null || StringUtils.isEmpty(invmbLog.getLastsynctime())) {   // 如果没记录，则是第一次同步
            viewList = hzViewSyncMapper.hzInvmbViewList(null);
        }else{
            viewList = hzViewSyncMapper.hzInvmbViewList(invmbLog.getLastsynctime());
        }
        List<String> originList = invmbMapper.mb001List();
        Set<String> originSet = new HashSet<>();
        for(String t : originList) originSet.add(t.trim());
        if(viewList != null && viewList.size() > 0) {
            Set<String> viewSet = new HashSet<>();
            Map<String, HzInvmbView> viewMap = new HashMap<>();
            String lastTime = "";
            for(int i = 0; i < viewList.size(); i++) {
                viewList.set(i, LanguageConversionUtils.convertToSimplifiedChinese(viewList.get(i)));
            }
            for(HzInvmbView item : viewList) {
                viewMap.put(item.getMB001().trim(), item);
                viewSet.add(item.getMB001().trim());
                if(lastTime == "") lastTime = item.getModified_time();
                else {
                    if(lastTime.compareTo(item.getModified_time()) < 0) {
                        lastTime = item.getModified_time();
                    }
                }
            }
            Set<String> cp = new HashSet<>(viewSet);
            cp.removeAll(originSet);
            List<Invmb> insInvmbList = new ArrayList<>();
            for(String t : cp) {  // 插入VIEW中多的
                HzInvmbView view = viewMap.get(t);
                Invmb invmb = new Invmb();
                BeanUtils.copyPropertiesIgnoreCase(view, invmb);
                switch (view.getSTATE()) {
                    case "未审核" : invmb.setMb109("y"); break;
                    case "已禁用" : invmb.setMb109("N"); break;
                    case "已审核" : invmb.setMb109("Y"); break;
                }
                insInvmbList.add(invmb);
            }
            // 批量插入
            invmbService.saveBatch(insInvmbList);
            // 批量更新
            cp = new HashSet<>(originSet);
            cp.retainAll(viewSet);
            for(String t : cp) {
                HzInvmbView view = viewMap.get(t);
                Invmb invmb = new Invmb();
                BeanUtils.copyPropertiesIgnoreCase(view, invmb);
                switch (view.getSTATE()) {
                    case "未审核" : invmb.setMb109("y"); break;
                    case "已禁用" : invmb.setMb109("N"); break;
                    case "已审核" : invmb.setMb109("Y"); break;
                }
                invmbMapper.update(invmb ,new LambdaQueryWrapper<Invmb>().eq(Invmb::getMb001, t));
            }
            if(invmbLog == null) {
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclog.setTablename("INVMB");
                tablesynclogService.save(tablesynclog);
            }else{
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclogService.update(tablesynclog, new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "INVMB"));
            }
        }
        System.out.println("INVMB 删除......" + TimeUtils.getFullNowTime());
        List<String> mb001View = hzViewSyncMapper.hzInvmbViewListOnlyMb001();
        Set<String> viewAllSet = new HashSet<>();
        for(String t : mb001View) viewAllSet.add(t);
        // 查看有没有VIEW中没有的，如果没有后续单据，需要删除。有的话，则禁用
        Set<String> cp = new HashSet<>(originSet);
        cp.removeAll(viewAllSet);
        for(String t : cp) {  // 删除我们这边有的，VIEW中没有的
            if(invmbMapper.havingLinkTable(t).equals(1)) {   // 有后续单据则把invmb改为 禁用
                Invmb invmb = new Invmb();  invmb.setMb019("N");
                invmbMapper.update(invmb ,new LambdaQueryWrapper<Invmb>().eq(Invmb::getMb001, t));
            }else{  // 否则删除
                invmbMapper.delete(new LambdaQueryWrapper<Invmb>().eq(Invmb::getMb001, t));
            }
        }
        System.out.println("invmb end....." + TimeUtils.getFullNowTime());

        System.out.println("bommfViewSync....." + TimeUtils.getFullNowTime());
        Tablesynclog bommfLog = tablesynclogService.getOne(new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "BOMMF"));
        List<HzBommfView> bommfViewList = null;
        if(bommfLog == null || StringUtils.isEmpty(bommfLog.getLastsynctime())) {   // 如果没记录，则是第一次同步
            bommfViewList = hzViewSyncMapper.hzBommfViewList(null);
        }else{
            bommfViewList = hzViewSyncMapper.hzBommfViewList(bommfLog.getLastsynctime());
        }
        if(bommfViewList != null && bommfViewList.size() > 0) {
            Set<HzBommfView> bommfViewSet = new HashSet<>();
            String lastTime = "";
            for(int i = 0; i < bommfViewList.size(); i++) {
                bommfViewList.set(i, LanguageConversionUtils.convertToSimplifiedChinese(bommfViewList.get(i)));
            }
            for(HzBommfView item : bommfViewList) {
                bommfViewSet.add(item);
                if(lastTime == "") lastTime = item.getModified_Time();
                else {
                    if(lastTime.compareTo(item.getModified_Time()) < 0) {
                        lastTime = item.getModified_Time();
                    }
                }
            }
            List<HzBommfView> originBommfList = bommfMapper.getOriginHzBommfView();
            Set<HzBommfView> originBommfSet = new HashSet<>();
            for(HzBommfView t : originBommfList) originBommfSet.add(t);

            Set<HzBommfView> bommfCp = new HashSet<>(bommfViewSet);
            bommfCp.removeAll(originBommfSet);
            List<Bommf> insBommfList = new ArrayList<>();
//            List<Bomme> insBommeList = new ArrayList<>();
            for(HzBommfView t : bommfCp) {  // 批量插入
                /**
                 *     // invrt_name 工艺类别
                 *     @TableField("UDF10")
                 *     private String udf10;
                 *     // inproductno 在产品编号
                 *     @TableField("UDF11")
                 *     private String udf11;
                 *     // IsDefault 是否为默认路线
                 *     @TableField("UDF12")
                 *     private String udf12;
                 */
                Bommf bommf = new Bommf();
                bommf.setCreateDate(TimeUtils.getNowTime());  bommf.setCreator("ERP");
//                bommf.setMf001(t.getMF001());
                bommf.setMf001(t.getInproductno().trim());
                bommf.setMf002(t.getMF002());  bommf.setMf003(t.getMF003());  bommf.setMf004(t.getMF004());  bommf.setMf005(t.getMF005());
                bommf.setMf006(t.getMF006());  bommf.setMf007(t.getMF007());

                bommf.setUdf10(t.getInvrt_name());

                bommf.setUdf11(t.getMF001().trim());

                bommf.setUdf12(t.getIsDefault());
                bommf.setUdf09(t.getME003().trim());
                insBommfList.add(bommf);

                Bomme bomme = bommeMapper.selectOne(new LambdaQueryWrapper<Bomme>().eq(Bomme::getMe001, t.getInproductno()).eq(Bomme::getMe002, t.getMF002()));
                if(bomme == null) {  // 如果单头没有，则单头也需要插入
                    bomme = new Bomme();
                    bomme.setCreator("ERP");  bomme.setCreateDate(TimeUtils.getNowTime());
                    bomme.setMe001(t.getInproductno().trim());  bomme.setMe002(t.getMF002());  bomme.setMe003(t.getME003());
//                    insBommeList.add(bomme);
                    bommeMapper.insert(bomme);
                }
            }
            bommfService.saveBatch(insBommfList);
//            bommeService.saveBatch(insBommeList);

            bommfCp = new HashSet<>(bommfViewSet);
            bommfCp.retainAll(originBommfSet);
            for(HzBommfView t : bommfCp) {  // 批量修改
                Bommf bommf = new Bommf();
                bommf.setModiDate(TimeUtils.getNowTime());  bommf.setModifier("ERP");
//                bommf.setMf001(t.getMF001());
                bommf.setMf001(t.getInproductno().trim());
                bommf.setMf002(t.getMF002());  bommf.setMf003(t.getMF003());  bommf.setMf004(t.getMF004());  bommf.setMf005(t.getMF005());
                bommf.setMf006(t.getMF006());  bommf.setMf007(t.getMF007());
                bommf.setUdf10(t.getInvrt_name());
//                bommf.setUdf11(t.getInproductno());
                bommf.setUdf11(t.getMF001().trim());
                bommf.setUdf12(t.getIsDefault());
                bommf.setUdf09(t.getME003().trim());
                Bommf one = bommfMapper.selectOne(new LambdaQueryWrapper<Bommf>().eq(Bommf::getMf001, t.getInproductno())
                        .eq(Bommf::getMf002, t.getMF002()).eq(Bommf::getMf003, t.getMF003()).
                        eq(Bommf::getUdf11, t.getMF001().trim()).
                        eq(Bommf::getUdf12, t.getIsDefault()).eq(Bommf::getUdf10, t.getInvrt_name()));
                if(one != null) bommfMapper.update(bommf, new LambdaQueryWrapper<Bommf>().eq(Bommf::getMf001, t.getInproductno())
                        .eq(Bommf::getMf002, t.getMF002()).eq(Bommf::getMf003, t.getMF003()).eq(Bommf::getUdf11, t.getMF001().trim())
                        .eq(Bommf::getUdf12, t.getIsDefault()).eq(Bommf::getUdf10, t.getInvrt_name()));
            }

            if(bommfLog == null) {
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclog.setTablename("BOMMF");
                tablesynclogService.save(tablesynclog);
            }else{
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclogService.update(tablesynclog, new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "BOMMF"));
            }
        }
        System.out.println("bommf end....." + TimeUtils.getFullNowTime());
    }

    @Scheduled(cron = "0 0/30 * * * ?")
    @DSTransactional
    public void cmsmeAndCmsmvViewSync() {
        System.out.println("active = " + active);
        if(!"hz".equals(active)) {
            return;
        }
        System.out.println("cmsmeViewSync....." + TimeUtils.getFullNowTime());
        Tablesynclog cmsmeLog = tablesynclogService.getOne(new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "CMSME"));
        List<HzCmsmeView> viewList = null;
        if(cmsmeLog == null || StringUtils.isEmpty(cmsmeLog.getLastsynctime())) {   // 如果没记录，则是第一次同步
            viewList = hzViewSyncMapper.hzCmsmeViewList(null);
        }else{
            viewList = hzViewSyncMapper.hzCmsmeViewList(cmsmeLog.getLastsynctime());
        }
        if(viewList != null && viewList.size() > 0) {
            String lastTime = "";
            for(int i = 0; i < viewList.size(); i++) {
                viewList.set(i, LanguageConversionUtils.convertToSimplifiedChinese(viewList.get(i)));
                if(lastTime == "") lastTime = viewList.get(i).getModified_time();
                else {
                    if(lastTime.compareTo(viewList.get(i).getModified_time()) < 0) {
                        lastTime = viewList.get(i).getModified_time();
                    }
                }
            }
            Set<String> viewSet = new HashSet<>();
            Map<String, HzCmsmeView> viewMap = new HashMap<>();
            for(HzCmsmeView t : viewList) {
                viewSet.add(t.getME001().trim());
                viewMap.put(t.getME001().trim(), t);
            }
            List<Cmsme> cmsmeList = cmsmeMapper.selectList(null);
            Set<String> cmsmeSet = new HashSet<>();
            for(Cmsme cmsme : cmsmeList) {
                cmsmeSet.add(cmsme.getMe001().trim());
            }
            Set<String> cp = new HashSet<>(viewSet);
            cp.removeAll(cmsmeSet);

            List<Cmsme> insCmsmeList = new ArrayList<>();
            for(String t : cp) {  // 插入VIEW中多的
                HzCmsmeView view = viewMap.get(t);
                Cmsme cmsme = new Cmsme();
                BeanUtils.copyPropertiesIgnoreCase(view, cmsme);
                switch (view.getSTATE()) {
                    case "启用" : cmsme.setMe008("Y"); break;
                    case "禁用" : cmsme.setMe008("N"); break;
                }
                insCmsmeList.add(cmsme);
            }
            // 批量插入
            cmsmeService.saveBatch(insCmsmeList);
            // 批量更新
            cp = new HashSet<>(cmsmeSet);
            cp.retainAll(viewSet);
            for(String t : cp) {
                HzCmsmeView view = viewMap.get(t);
                Cmsme cmsme = new Cmsme();
                BeanUtils.copyPropertiesIgnoreCase(view, cmsme);
                switch (view.getSTATE()) {
                    case "启用" : cmsme.setMe008("Y"); break;
                    case "禁用" : cmsme.setMe008("N"); break;
                }
                cmsmeMapper.update(cmsme ,new LambdaQueryWrapper<Cmsme>().eq(Cmsme::getMe001, t));
            }
            // 批量删除
            List<String> me001View = hzViewSyncMapper.hzCmsmeViewListOnlyMe001();
            Set<String> viewAllSet = new HashSet<>();
            for(String t : me001View) viewAllSet.add(t);
            // 查看有没有VIEW中没有的，如果没有后续单据，需要删除。有的话，则禁用
            cp = new HashSet<>(cmsmeSet);
            cp.removeAll(viewAllSet);
            for(String t : cp) {  // 删除我们这边有的，VIEW中没有的
                cmsmeMapper.delete(new LambdaQueryWrapper<Cmsme>().eq(Cmsme::getMe001, t));
            }
            if(cmsmeLog == null) {
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclog.setTablename("CMSME");
                tablesynclogService.save(tablesynclog);
            }else{
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclogService.update(tablesynclog, new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "CMSME"));
            }
        }
        System.out.println("cmsmeViewSync end....." + TimeUtils.getFullNowTime());

        System.out.println("cmsmvViewSync....." + TimeUtils.getFullNowTime());
        // TODO
        Tablesynclog cmsmvLog = tablesynclogService.getOne(new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "CMSMV"));
        List<HzCmsmvView> cmsmvViewList = null;
        if(cmsmvLog == null || StringUtils.isEmpty(cmsmvLog.getLastsynctime())) {   // 如果没记录，则是第一次同步
            cmsmvViewList = hzViewSyncMapper.hzCmsmvViewList(null);
        }else{
            cmsmvViewList = hzViewSyncMapper.hzCmsmvViewList(cmsmvLog.getLastsynctime());
        }
        if(cmsmvViewList != null && cmsmvViewList.size() > 0) {
            String lastTime = "";
            for(int i = 0; i < cmsmvViewList.size(); i++) {
                cmsmvViewList.set(i, LanguageConversionUtils.convertToSimplifiedChinese(cmsmvViewList.get(i)));
                if(lastTime == "") lastTime = cmsmvViewList.get(i).getModified_time();
                else {
                    if(lastTime.compareTo(cmsmvViewList.get(i).getModified_time()) < 0) {
                        lastTime = cmsmvViewList.get(i).getModified_time();
                    }
                }
            }
            Set<String> viewSet = new HashSet<>();
            Map<String, HzCmsmvView> viewMap = new HashMap<>();
            for(HzCmsmvView t : cmsmvViewList) {
                viewSet.add(t.getMV001());
                viewMap.put(t.getMV001(), t);
            }
            List<Cmsmv> cmsmvList = cmsmvMapper.selectList(null);
            Set<String> cmsmvSet = new HashSet<>();
            for(Cmsmv cmsmv : cmsmvList) {
                cmsmvSet.add(cmsmv.getMv001().trim());
            }
            Set<String> cp = new HashSet<>(viewSet);
            cp.removeAll(cmsmvSet);

            List<Cmsme> cmsmeList = cmsmeMapper.selectList(null);
            Map<String, String> departmentMap = new HashMap<>();
            for(Cmsme cmsme : cmsmeList) {
                departmentMap.put(cmsme.getMe002().trim(), cmsme.getMe001().trim());
            }
            List<Cmsmv> insCmsmvList = new ArrayList<>();
            for(String t : cp) {  // 插入VIEW中多的
                HzCmsmvView view = viewMap.get(t);
                Cmsmv cmsmv = new Cmsmv();
                BeanUtils.copyPropertiesIgnoreCase(view, cmsmv);
                cmsmv.setMv004(departmentMap.get(view.getMV004()));
                switch (view.getSTATE()) {
                    case "在职" : cmsmv.setMv066("Y"); break;
                    case "离职" : cmsmv.setMv066("N"); break;
                }
                insCmsmvList.add(cmsmv);
            }
            // 批量插入
            cmsmvService.saveBatch(insCmsmvList);
            // 批量更新
            cp = new HashSet<>(cmsmvSet);
            cp.retainAll(viewSet);
            for(String t : cp) {
                HzCmsmvView view = viewMap.get(t);
                Cmsmv cmsmv = new Cmsmv();
                BeanUtils.copyPropertiesIgnoreCase(view, cmsmv);
                cmsmv.setMv004(departmentMap.get(view.getMV004()));
                switch (view.getSTATE()) {
                    case "在职" : cmsmv.setMv066("Y"); break;
                    case "离职" : cmsmv.setMv066("N"); break;
                }
                cmsmvMapper.update(cmsmv ,new LambdaQueryWrapper<Cmsmv>().eq(Cmsmv::getMv001, t));
            }
            // 批量删除
            List<String> mv001View = hzViewSyncMapper.hzCmsmvViewListOnlyMv001();
            Set<String> viewAllSet = new HashSet<>();
            for(String t : mv001View) viewAllSet.add(t);
            // 查看有没有VIEW中没有的，如果没有后续单据，需要删除。有的话，则禁用
            cp = new HashSet<>(cmsmvSet);
            cp.removeAll(viewAllSet);
            for(String t : cp) {  // 删除我们这边有的，VIEW中没有的
                cmsmvMapper.delete(new LambdaQueryWrapper<Cmsmv>().eq(Cmsmv::getMv001, t));
            }
            if(cmsmvLog == null) {
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclog.setTablename("CMSMV");
                tablesynclogService.save(tablesynclog);
            }else{
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclogService.update(tablesynclog, new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "CMSMV"));
            }
        }
        System.out.println("cmsmvViewSync end....." + TimeUtils.getFullNowTime());

    }

    @Scheduled(cron = "0 0/30 * * * ?")
    @DSTransactional
    public void cmsmcViewSync() {
        System.out.println("active = " + active);
        if(!"hz".equals(active)) {
            return;
        }
        System.out.println("cmsmcSync....." + TimeUtils.getFullNowTime());
        Tablesynclog cmsmcLog = tablesynclogService.getOne(new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "CMSMC"));
        List<HzCmsmcView> viewList = null;
        if(cmsmcLog == null || StringUtils.isEmpty(cmsmcLog.getLastsynctime())) {   // 如果没记录，则是第一次同步
            viewList = hzViewSyncMapper.hzCmsmcViewList(null);
        }else{
            viewList = hzViewSyncMapper.hzCmsmcViewList(cmsmcLog.getLastsynctime());
        }
        if(viewList != null && viewList.size() > 0) {
            List<String> originList = cmsmcMapper.mc001List();
            Set<String> originSet = new HashSet<>();
            for(String t : originList) originSet.add(t.trim());
            Set<String> viewSet = new HashSet<>();
            Map<String, HzCmsmcView> viewMap = new HashMap<>();
            String lastTime = "";
            for(int i = 0; i < viewList.size(); i++) {
                viewList.set(i, LanguageConversionUtils.convertToSimplifiedChinese(viewList.get(i)));
            }
            for(HzCmsmcView item : viewList) {
                viewMap.put(item.getMC001(), item);
                viewSet.add(item.getMC001());
                if(lastTime == "") lastTime = item.getModified_Time();
                else {
                    if(lastTime.compareTo(item.getModified_Time()) < 0) {
                        lastTime = item.getModified_Time();
                    }
                }
            }
            Set<String> cp = new HashSet<>(viewSet);
            cp.removeAll(originSet);
            List<Cmsmc> insCmsmcList = new ArrayList<>();
            for(String t : cp) {  // 插入VIEW中多的
                HzCmsmcView view = viewMap.get(t);
                Cmsmc cmsmc = new Cmsmc();
                BeanUtils.copyPropertiesIgnoreCase(view, cmsmc);
                switch (view.getSTATE()) {
                    case "启用" : cmsmc.setMc018("Y"); break;
                    case "禁用" : cmsmc.setMc018("N"); break;
                }
                insCmsmcList.add(cmsmc);
            }
            // 批量插入
            cmsmcService.saveBatch(insCmsmcList);
            // 批量更新
            cp = new HashSet<>(originSet);
            cp.retainAll(viewSet);
            for(String t : cp) {
                HzCmsmcView view = viewMap.get(t);
                Cmsmc cmsmc = new Cmsmc();
                BeanUtils.copyPropertiesIgnoreCase(view, cmsmc);
                switch (view.getSTATE()) {
                    case "启用" : cmsmc.setMc018("Y"); break;
                    case "禁用" : cmsmc.setMc018("N"); break;
                }
                BeanUtils.copyPropertiesIgnoreCase(view, cmsmc);
                cmsmcMapper.update(cmsmc ,new LambdaQueryWrapper<Cmsmc>().eq(Cmsmc::getMc001, t));
            }
            List<String> mc001View = hzViewSyncMapper.hzCmsmcViewListOnlyMc001();
            Set<String> viewAllSet = new HashSet<>();
            for(String t : mc001View) viewAllSet.add(t);
            // 查看有没有VIEW中没有的，如果没有后续单据，需要删除。有的话，则禁用
            cp = new HashSet<>(originSet);
            cp.removeAll(viewAllSet);
            for(String t : cp) {  // 删除我们这边有的，VIEW中没有的
                cmsmcMapper.delete(new LambdaQueryWrapper<Cmsmc>().eq(Cmsmc::getMc001, t));
            }
            if(cmsmcLog == null) {
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclog.setTablename("CMSMC");
                tablesynclogService.save(tablesynclog);
            }else{
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclogService.update(tablesynclog, new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "CMSMC"));
            }
        }
        System.out.println("CMSMC end....." + TimeUtils.getFullNowTime());
    }

    @Scheduled(cron = "0 0/30 * * * ?")
    @DSTransactional
    public void cmsmdViewSync() {
        System.out.println("active = " + active);
        if(!"hz".equals(active)) {
            return;
        }
        System.out.println("cmsmdViewSync....." + TimeUtils.getFullNowTime());
        Tablesynclog cmsmdLog = tablesynclogService.getOne(new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "CMSMD"));
        List<HzCmsmdView> viewList = null;
        if(cmsmdLog == null || StringUtils.isEmpty(cmsmdLog.getLastsynctime())) {   // 如果没记录，则是第一次同步
            viewList = hzViewSyncMapper.hzCmsmdViewList(null);
        }else{
            viewList = hzViewSyncMapper.hzCmsmdViewList(cmsmdLog.getLastsynctime());
        }
        if(viewList != null && viewList.size() > 0) {
            List<String> originList = cmsmdMapper.md001List();
            Set<String> originSet = new HashSet<>();
            for(String t : originList) originSet.add(t.trim());
            Set<String> viewSet = new HashSet<>();
            Map<String, HzCmsmdView> viewMap = new HashMap<>();
            String lastTime = "";
            for(int i = 0; i < viewList.size(); i++) {
                viewList.set(i, LanguageConversionUtils.convertToSimplifiedChinese(viewList.get(i)));
            }
            for(HzCmsmdView item : viewList) {
                viewMap.put(item.getMD001(), item);
                viewSet.add(item.getMD001());
                if(lastTime == "") lastTime = item.getModified_time();
                else {
                    if(lastTime.compareTo(item.getModified_time()) < 0) {
                        lastTime = item.getModified_time();
                    }
                }
            }
            Set<String> cp = new HashSet<>(viewSet);
            cp.removeAll(originSet);
            List<Cmsmd> insCmsmdList = new ArrayList<>();
            for(String t : cp) {  // 插入VIEW中多的
                HzCmsmdView view = viewMap.get(t);
                Cmsmd cmsmd = new Cmsmd();
                BeanUtils.copyPropertiesIgnoreCase(view, cmsmd);
                insCmsmdList.add(cmsmd);
            }
            // 批量插入
            cmsmdService.saveBatch(insCmsmdList);
            // 批量更新
            cp = new HashSet<>(originSet);
            cp.retainAll(viewSet);
            for(String t : cp) {
                HzCmsmdView view = viewMap.get(t);
                Cmsmd cmsmd = new Cmsmd();
                BeanUtils.copyPropertiesIgnoreCase(view, cmsmd);
                cmsmdMapper.update(cmsmd, new LambdaQueryWrapper<Cmsmd>().eq(Cmsmd::getMd001, cmsmd.getMd001()));
            }
            if(cmsmdLog == null) {
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclog.setTablename("CMSMD");
                tablesynclogService.save(tablesynclog);
            }else{
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclogService.update(tablesynclog, new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "CMSMD"));
            }
        }
        List<String> originList = cmsmdMapper.md001List();
        Set<String> originSet = new HashSet<>();
        for(String t : originList) originSet.add(t.trim());
        List<String> md001View = hzViewSyncMapper.hzCmsmdViewListOnlyMd001();
        Set<String> viewAllSet = new HashSet<>();
        for(String t : md001View) viewAllSet.add(t);
        // 查看有没有VIEW中没有的，如果没有后续单据，需要删除。有的话，则禁用
        Set<String> cp = new HashSet<>(originSet);
        cp.removeAll(viewAllSet);
        for(String t : cp) {  // 删除我们这边有的，VIEW中没有的
            cmsmdMapper.delete(new LambdaQueryWrapper<Cmsmd>().eq(Cmsmd::getMd001, t));
        }
        System.out.println("CMSMD end....." + TimeUtils.getFullNowTime());
    }

    @Scheduled(cron = "0 0/30 * * * ?")
    @DSTransactional
    public void purmaViewSync() {
        System.out.println("active = " + active);
        if(!"hz".equals(active)) {
            return;
        }
        System.out.println("purmaViewSync....." + TimeUtils.getFullNowTime());
        Tablesynclog purmaLog = tablesynclogService.getOne(new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "PURMA"));
        List<HzPurmaView> viewList = null;
        if(purmaLog == null || StringUtils.isEmpty(purmaLog.getLastsynctime())) {   // 如果没记录，则是第一次同步
            viewList = hzViewSyncMapper.hzPurmaViewList(null);
        }else{
            viewList = hzViewSyncMapper.hzPurmaViewList(purmaLog.getLastsynctime());
        }
        if(viewList != null && viewList.size() > 0) {
            List<String> originList = purmaMapper.ma001List();
            Set<String> originSet = new HashSet<>();
            for(String t : originList) originSet.add(t.trim());
            Set<String> viewSet = new HashSet<>();
            Map<String, HzPurmaView> viewMap = new HashMap<>();
            String lastTime = "";
            for(int i = 0; i < viewList.size(); i++) {
                viewList.set(i, LanguageConversionUtils.convertToSimplifiedChinese(viewList.get(i)));
            }
            for(HzPurmaView item : viewList) {
                viewMap.put(item.getMA001(), item);
                viewSet.add(item.getMA001());
                if(lastTime == "") lastTime = item.getModified_Time();
                else {
                    if(lastTime.compareTo(item.getModified_Time()) < 0) {
                        lastTime = item.getModified_Time();
                    }
                }
            }
            Set<String> cp = new HashSet<>(viewSet);
            cp.removeAll(originSet);
            List<Purma> insPurmaList = new ArrayList<>();
            for(String t : cp) {  // 插入VIEW中多的
                HzPurmaView view = viewMap.get(t);
                Purma purma = new Purma();
                BeanUtils.copyPropertiesIgnoreCase(view, purma);
                switch (view.getSTATE()) {
                    case "启用" : purma.setMa016("1"); break;
                    case "禁用" : purma.setMa016("3"); break;
                }
                insPurmaList.add(purma);
            }
            // 批量插入
            purmaService.saveBatch(insPurmaList);
            // 批量更新
            cp = new HashSet<>(originSet);
            cp.retainAll(viewSet);
            for(String t : cp) {
                HzPurmaView view = viewMap.get(t);
                Purma purma = new Purma();
                BeanUtils.copyPropertiesIgnoreCase(view, purma);
                switch (view.getSTATE()) {
                    case "启用" : purma.setMa016("1"); break;
                    case "禁用" : purma.setMa016("3"); break;
                }
                purmaMapper.update(purma, new LambdaQueryWrapper<Purma>().eq(Purma::getMa001, t));
            }
            List<String> ma001View = hzViewSyncMapper.hzPurmaViewListOnlyMa001();
            Set<String> viewAllSet = new HashSet<>();
            for(String t : ma001View) viewAllSet.add(t);
            // 查看有没有VIEW中没有的，如果没有后续单据，需要删除。有的话，则禁用
            cp = new HashSet<>(originSet);
            cp.removeAll(viewAllSet);
            for(String t : cp) {  // 删除我们这边有的，VIEW中没有的
                purmaMapper.delete(new LambdaQueryWrapper<Purma>().eq(Purma::getMa001, t));
            }
            if(purmaLog == null) {
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclog.setTablename("PURMA");
                tablesynclogService.save(tablesynclog);
            }else{
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclogService.update(tablesynclog, new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "PURMA"));
            }
        }
        System.out.println("purma end....." + TimeUtils.getFullNowTime());
    }

    @Scheduled(cron = "0 0/30 * * * ?")
    @DSTransactional
    public void copmaViewSync() {
        System.out.println("active = " + active);
        if(!"hz".equals(active)) {
            return;
        }
        System.out.println("copmaViewSync....." + TimeUtils.getFullNowTime());
        Tablesynclog copmaLog = tablesynclogService.getOne(new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "COPMA"));
        List<HzCopmaView> viewList = null;
        if(copmaLog == null || StringUtils.isEmpty(copmaLog.getLastsynctime())) {   // 如果没记录，则是第一次同步
            viewList = hzViewSyncMapper.hzCopmaViewList(null);
        }else{
            viewList = hzViewSyncMapper.hzCopmaViewList(copmaLog.getLastsynctime());
        }
        if(viewList != null && viewList.size() > 0) {
            List<String> originList = copmaMapper.ma001List();
            Set<String> originSet = new HashSet<>();
            for(String t : originList) originSet.add(t.trim());
            Set<String> viewSet = new HashSet<>();
            Map<String, HzCopmaView> viewMap = new HashMap<>();
            String lastTime = "";
            for(int i = 0; i < viewList.size(); i++) {
                viewList.set(i, LanguageConversionUtils.convertToSimplifiedChinese(viewList.get(i)));
            }
            for(HzCopmaView item : viewList) {
                viewMap.put(item.getMA001(), item);
                viewSet.add(item.getMA001());
                if(lastTime == "") lastTime = item.getModified_Time();
                else {
                    if(lastTime.compareTo(item.getModified_Time()) < 0) {
                        lastTime = item.getModified_Time();
                    }
                }
            }
            Set<String> cp = new HashSet<>(viewSet);
            cp.removeAll(originSet);
            List<Copma> insCopmaList = new ArrayList<>();
            for(String t : cp) {  // 插入VIEW中多的
                HzCopmaView view = viewMap.get(t);
                Copma copma = new Copma();
                BeanUtils.copyPropertiesIgnoreCase(view, copma);
                insCopmaList.add(copma);
            }
            // 批量插入
            copmaService.saveBatch(insCopmaList);
            // 批量更新
            cp = new HashSet<>(originSet);
            cp.retainAll(viewSet);
            for(String t : cp) {
                HzCopmaView view = viewMap.get(t);
                Copma copma = new Copma();
                BeanUtils.copyPropertiesIgnoreCase(view, copma);
                copmaMapper.update(copma, new LambdaQueryWrapper<Copma>().eq(Copma::getMa001, t));
            }
            List<String> ma001View = hzViewSyncMapper.hzCopmaViewListOnlyMa001();
            Set<String> viewAllSet = new HashSet<>();
            for(String t : ma001View) viewAllSet.add(t);
            // 查看有没有VIEW中没有的，如果没有后续单据，需要删除。有的话，则禁用
            cp = new HashSet<>(originSet);
            cp.removeAll(viewAllSet);
            for(String t : cp) {  // 删除我们这边有的，VIEW中没有的
                copmaMapper.delete(new LambdaQueryWrapper<Copma>().eq(Copma::getMa001, t));
            }
            if(copmaLog == null) {
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclog.setTablename("COPMA");
                tablesynclogService.save(tablesynclog);
            }else{
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclogService.update(tablesynclog, new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "COPMA"));
            }
        }
        System.out.println("copma end....." + TimeUtils.getFullNowTime());
    }

    @Scheduled(cron = "0 0/25 * * * ?")
    @DSTransactional
    public void resourceGroup_ResourceViewSync() {
        System.out.println("active = " + active);
        if(!"hz".equals(active)) {
            return;
        }
        System.out.println("resourceGroup_ResourceViewSync start.........");
        List<HzResourceGroupView> resourceGroupViewList = null;
        Tablesynclog resourcegroupLog = tablesynclogService.getOne(new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "resourcegroup"));
        if(resourcegroupLog == null || StringUtils.isEmpty(resourcegroupLog.getLastsynctime())) {   // 如果没记录，则是第一次同步
            resourceGroupViewList = hzViewSyncMapper.hzResourceGroupViewList(null);
        }else{
            resourceGroupViewList = hzViewSyncMapper.hzResourceGroupViewList(resourcegroupLog.getLastsynctime());
        }

        if(resourceGroupViewList != null && resourceGroupViewList.size() > 0) {
            Set<String> groupNoSet = new HashSet<>();   // 视图中的组编号
            Map<String, Integer> groupNoMap = new HashMap<>();  // 视图中的组编号对应的 -- 1，2，3，4
            Map<String, String> groupNoNameMap = new HashMap<>();
            String lastTime = "";
            for(int i = 0; i < resourceGroupViewList.size(); i++) {
                resourceGroupViewList.set(i, LanguageConversionUtils.convertToSimplifiedChinese(resourceGroupViewList.get(i)));
            }
            for(HzResourceGroupView item : resourceGroupViewList) {
                if(lastTime == "") lastTime = item.getModified_time();
                else {
                    if(lastTime.compareTo(item.getModified_time()) < 0) {
                        lastTime = item.getModified_time();
                    }
                }
            }

            Set<String> jutiSet = new HashSet<>();   // 视图中的具体资源组编号 F
            Map<String, HzResourceGroupView> jutiSetMap = new HashMap<>();  // 视图中的组编号对应的
            for(HzResourceGroupView t : resourceGroupViewList) {
                if(t.getResourceType().equals(1)) {
                    jutiSet.add(t.getResourceGroupNO().trim() + "(" + t.getWorkCenter().trim() + ")");
                    jutiSetMap.put(t.getResourceGroupNO().trim() + "(" + t.getWorkCenter().trim() + ")", t);
                }else{
                    jutiSet.add(t.getResourceGroupNO().trim());
                    jutiSetMap.put(t.getResourceGroupNO().trim(), t);
                }
                if(StringUtils.isNotEmpty(t.getGroupNo().trim())) {
                    groupNoSet.add(t.getGroupNo().trim());
                    groupNoMap.put(t.getGroupNo().trim(), t.getResourceType());
                    groupNoNameMap.put(t.getGroupNo().trim(), t.getGroupName().trim());
                }
            }
            List<Resourcegroup> groupList = resourcegroupService.list();
            Set<String> noSet = new HashSet<>();    // 所有现在表中已存在的 no
            Map<String, Resourcegroup> allItemMap = new HashMap<>();  // 所有Item和实体类的映射
            for(Resourcegroup t : groupList) {
                noSet.add(t.getResourceGroupNO().trim());
                allItemMap.put(t.getResourceGroupNO().trim(), t);
            }
            Set<String> cp = new HashSet<>(groupNoSet);
            cp.removeAll(noSet);
            if(cp.size() > 0) {   // 如果有不一样的，就是需要创建 M     CT-HD-3462 面板下盖
                for(String t : cp) {
                    if(StringUtils.isEmpty(t)) continue;
                    Resourcegroup resourcegroup = new Resourcegroup();
                    resourcegroup.setType("M");  resourcegroup.setResourceGroupParentId(groupNoMap.get(t));
                    resourcegroup.setResourceGroupNO(t); resourcegroup.setResourceGroupName(groupNoNameMap.get(t));
                    resourcegroupService.save(resourcegroup);
//                  System.out.println("已保存的resourcegroup: " + resourcegroup);
                    if(resourcegroup.getResourceGroupId() == null) throw new CustomException("500", "resourcegroup M插入后没有给ID");
                    allItemMap.put(t.trim(), resourcegroup);  // ???
                }
            }
            cp = new HashSet<>(jutiSet);
            cp.removeAll(noSet);
            if(cp.size() > 0) {
                for(String t : cp) {
                    HzResourceGroupView view = jutiSetMap.get(t);
                    Resourcegroup resourcegroup = new Resourcegroup();
                    resourcegroup.setType("F");
                    if(StringUtils.isNotEmpty(view.getGroupNo())) {
//                        System.out.println("view.getGroupNo = ^" + view.getGroupNo() + "^");
                        Resourcegroup resourcegroup1 = allItemMap.get(view.getGroupNo().trim());
//                        if(resourcegroup1 == null) System.out.println("resourcegroup1 is null");
                        Integer i = resourcegroup1.getResourceGroupId();
//                        if(i == null) System.out.println("iiii is null");
                        resourcegroup.setResourceGroupParentId(i);
                    } else resourcegroup.setResourceGroupParentId(view.getResourceType());
                    resourcegroup.setResourceGroupNO(t);
                    resourcegroup.setResourceGroupName(view.getResourceGroupName().trim());
                    resourcegroupService.save(resourcegroup);
                    if(resourcegroup.getResourceGroupId() == null) throw new CustomException("500", "resourcegroup F插入后没有给ID");
                    allItemMap.put(resourcegroup.getResourceGroupNO(), resourcegroup);
                    Resource resource = new Resource();
                    resource.setResourceType(view.getResourceType());
                    resource.setResourceGroupIdIsF(resourcegroup.getResourceGroupId());
                    resource.setResourceGroupId(resourcegroup.getResourceGroupParentId());
                    resource.setDeviceid(resourcegroup.getResourceGroupNO());
                    resource.setDevicename(resourcegroup.getResourceGroupName());
                    resource.setWorkCenter(view.getWorkCenter());
                    resource.setMouldDrawNo(view.getMouldDrawNo());
                    String status = "";
                    switch (view.getSTATE()) {
                        case "启用": status = MouldStatusEnum.XianZhi.getValue(); break;
                        case "已停用": status = MouldStatusEnum.YiTingYong.getValue(); break;
                        case "已审核": status = MouldStatusEnum.YiShenHe.getValue(); break;
                        case "开模中": status = MouldStatusEnum.KaiMoZhong.getValue(); break;
                        case "已报废": status = MouldStatusEnum.YiBaoFei.getValue(); break;
                        case "已作废": status = MouldStatusEnum.YiZuoFei.getValue(); break;
                        case "未审核": status = MouldStatusEnum.WeiShenHe.getValue(); break;
                    }
                    resource.setStatus(status);
                    resource.setDevicestatus(view.getSTATE());
                    resourceService.save(resource);
                }
            }

            cp = new HashSet<>(noSet);
            cp.retainAll(jutiSet);
            for(String t : cp) {        // 资源组修改  资源表修改

                HzResourceGroupView view = jutiSetMap.get(t);
                Resourcegroup groupInfo = null;
                if(StringUtils.isNotEmpty(view.getGroupNo())) groupInfo = allItemMap.get(view.getGroupNo().trim());   // 组编号

                Resourcegroup item = new Resourcegroup();
                item.setType(view.getType());
                item.setResourceGroupName(view.getResourceGroupName());
                item.setResourceGroupParentId(groupInfo == null ? view.getResourceType() : groupInfo.getResourceGroupId());
                resourcegroupMapper.update(item, new LambdaQueryWrapper<Resourcegroup>().eq(Resourcegroup::getResourceGroupNO, t));

                Resource resource = new Resource();
                resource.setResourceType(view.getResourceType());
                resource.setResourceGroupId(groupInfo == null ? view.getResourceType() : groupInfo.getResourceGroupId());
                resource.setDevicename(view.getResourceGroupName());
                resource.setWorkCenter(view.getWorkCenter());
//                String status = "";
//                switch (view.getSTATE()) {
//                    case "启用": status = MouldStatusEnum.XianZhi.getValue(); break;
//                    case "已停用": status = MouldStatusEnum.YiTingYong.getValue(); break;
//                    case "已审核": status = MouldStatusEnum.YiShenHe.getValue(); break;
//                    case "开模中": status = MouldStatusEnum.KaiMoZhong.getValue(); break;
//                    case "已报废": status = MouldStatusEnum.YiBaoFei.getValue(); break;
//                    case "已作废": status = MouldStatusEnum.YiZuoFei.getValue(); break;
//                    case "未审核": status = MouldStatusEnum.WeiShenHe.getValue(); break;
//                }
                resource.setDevicestatus(view.getSTATE()); // 不能更新status, 只能更新devicestatus
                resource.setMouldDrawNo(view.getMouldDrawNo());
                resourceMapper.update(resource, new LambdaQueryWrapper<Resource>().eq(Resource::getDeviceid, t));
            }

            // 删除掉，VIEW中不存在的内容
            List<String> viewNo = hzViewSyncMapper.hzResourceGroupListOnlyNO();
            Set<String> viewAllNo = new HashSet<>();
            // 需要先将编号进行繁体转简体
            for(String t : viewNo) {
                if(ZhTwConverterUtil.containsTraditional(t.trim())) {
                    t = ZhConvertBootstrap.newInstance().segment(Segments.chars())
                            .toSimple(t.trim());
                }
                viewAllNo.add(t.trim());
            }
            // 最外面四个的值，不能被删除
            viewAllNo.add("设备");
            viewAllNo.add("生产线");
            viewAllNo.add("模具");
            viewAllNo.add("工装");
//            System.out.println("allNo = " + allNo.size());
//            System.out.println("viewAllNo = " + viewAllNo.size());
            cp = new HashSet<>(noSet);
            cp.removeAll(viewAllNo);
//            System.out.println("cp = " + cp.size());
            List<Viewsynclog> viewsynclogList = new ArrayList<>();
            System.out.println("视图中的数量: " + viewAllNo.size());
            System.out.println("同步后的数量: " + noSet.size());
            System.out.println("需要删除的数量: " + cp.size());
            for(String t : cp) {  // 删除我们这边有的，VIEW中没有的 具体资源

                // TODO 如果有后置单据，则不能删除。把机器的状态改成 不可用

                // 删除的日志，记一下
                Viewsynclog viewsynclog = new Viewsynclog("resourcegroup", "DELETE", "删除编号:" + t, TimeUtils.getFullNowTime());
                viewsynclogList.add(viewsynclog);
                resourcegroupMapper.delete(new LambdaQueryWrapper<Resourcegroup>().eq(Resourcegroup::getResourceGroupNO, t));
                resourceMapper.delete(new LambdaQueryWrapper<Resource>().eq(Resource::getDeviceid, t));
                productprocessresourceMapper.delNoResourcegroupId();
            }
            viewsynclogService.saveBatch(viewsynclogList);
            if(resourcegroupLog == null) {
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclog.setTablename("resourcegroup");
                tablesynclogService.save(tablesynclog);
            }else{
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclogService.update(tablesynclog, new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "resourcegroup"));
            }

        }
        /**
         * resource  VIEW中只有模具的内容
         */
        List<HzResourceView> resourceViewList = null;
        Tablesynclog resourceLog = tablesynclogService.getOne(new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "resource"));
        if(resourceLog == null || StringUtils.isEmpty(resourceLog.getLastsynctime())) {   // 如果没记录，则是第一次同步
            resourceViewList = hzViewSyncMapper.hzResourceViewList(null);
        }else{
            resourceViewList = hzViewSyncMapper.hzResourceViewList(resourceLog.getLastsynctime());
        }
        if(resourceViewList != null && resourceViewList.size() > 0) {
            String lastTime = "";
            for(int i = 0; i < resourceViewList.size(); i++) {
                resourceViewList.set(i, LanguageConversionUtils.convertToSimplifiedChinese(resourceViewList.get(i)));
            }
            Set<String> resourceViewSet = new HashSet<>();
            Map<String, HzResourceView> resourceViewMap = new HashMap<>();

            List<Resourcegroup> resourcegroupList = resourcegroupService.list();
            Map<String, Resourcegroup> resourcegroupMap = new HashMap<>();
            for(Resourcegroup t : resourcegroupList) {
                resourcegroupMap.put(t.getResourceGroupNO().trim(), t);
            }

            for(HzResourceView item : resourceViewList) {
                if(lastTime == "") lastTime = item.getModified_time();
                else {
                    if(lastTime.compareTo(item.getModified_time()) < 0) {
                        lastTime = item.getModified_time();
                    }
                }
                resourceViewSet.add(item.getDeviceId().trim());
                resourceViewMap.put(item.getDeviceId().trim(), item);
            }
            List<Resource> resourceList = resourceService.list();
            Set<String> originResourceSet = new HashSet<>();
            Map<String, Resource> originResourceMap = new HashMap<>();
            for(Resource item : resourceList) {
                originResourceSet.add(item.getDeviceid().trim());
                originResourceMap.put(item.getDeviceid().trim(), item);
            }
            Set<String> cp = new HashSet<>(resourceViewSet);
            cp.removeAll(originResourceSet);
            List<Resource> insResourceList = new ArrayList<>();
            for(String t : cp) {
                HzResourceView view = resourceViewMap.get(t);
                Resourcegroup resourcegroup = resourcegroupMap.get(t);
                Resource resource = new Resource();
                resource.setResourceType(Integer.parseInt(view.getDeviceType()));   resource.setResourceGroupIdIsF(resourcegroup.getResourceGroupId());
                resource.setResourceGroupId(resourcegroup.getResourceGroupParentId());
                resource.setDeviceid(view.getDeviceId());  resource.setDevicename(view.getDeviceName());
                resource.setMouldDrawNo(view.getMouldDrawNo());  resource.setFixedassetid(view.getFixedAssetId());
                resource.setExfactorydate(view.getExfactoryDate());  resource.setRemark(view.getRemark());
                String status = "";
                switch (view.getDeviceStatus()) {
                    case "启用": status = MouldStatusEnum.XianZhi.getValue(); break;
                    case "已停用": status = MouldStatusEnum.YiTingYong.getValue(); break;
                    case "已审核": status = MouldStatusEnum.YiShenHe.getValue(); break;
                    case "开模中": status = MouldStatusEnum.KaiMoZhong.getValue(); break;
                    case "已报废": status = MouldStatusEnum.YiBaoFei.getValue(); break;
                    case "已作废": status = MouldStatusEnum.YiZuoFei.getValue(); break;
                    case "未审核": status = MouldStatusEnum.WeiShenHe.getValue(); break;
                }
                resource.setStatus(status);
                resource.setDevicestatus(view.getDeviceStatus());
                resource.setCavityNumber(view.getCavityNumber());
                resource.setCavitySpecifications(view.getCavitySpecifications());
                resource.setDesignLife(view.getDesignLife());
                resource.setUsedNumber(view.getUsedNumber());
                resource.setStorageLocation(view.getStorageLocation());

                resource.setFactoryMouldDrawingNumber(view.getFactoryMouldDrawingNumber());
                resource.setFactoryMouldDrawingVersion(view.getFactoryMouldDrawingVersion());
                resource.setCustomMouldDrawingNumber(view.getCustomMouldDrawingNumber());
                resource.setMouldType(view.getMouldType());
                resource.setWeight(view.getWeight());
                resource.setNameplateNumber(view.getNameplateNumber());
                resource.setOwnerName(view.getOwnerName());
                resource.setAssetFactory(view.getAssetFactory());
                resource.setLocationNumber(view.getLocationNumber());
                resource.setLocationName(view.getLocationName());
                resource.setModelStorageLocation(view.getModelStorageLocation());
                resource.setModelAdditionalInformation(view.getModelAdditionalInformation());
                resource.setMouldOpenOrderDay(view.getMouldOpenOrderDay());
                resource.setMouldOpenDay(view.getMouldOpenDay());
                resource.setExpectedCompletionDateOfMouldOpen(view.getExpectedCompletionDateOfMouldOpen());
                resource.setWarrantyExpirationDate(view.getWarrantyExpirationDate());
                resource.setInjectionMachineOunces(view.getInjectionMachineOunces());
                resource.setMachinePipeDiameter(view.getMachinePipeDiameter());
                resource.setSafeDesignLife(view.getSafeDesignLife());

                resource.setScrapDate(view.getScrapDate());
                resource.setScrapReason(view.getScrapReason());
                resource.setDeactivationDate(view.getDeactivationDate());
                resource.setDeactivationReason(view.getDeactivationReason());
                resource.setReactivationDay(view.getReactivationDay());
                resource.setInvMaterial(view.getInvMaterial());

                resource.setSpecification(view.getSpecification());  // 规格(模具规格)
                insResourceList.add(resource);
            }
            resourceService.saveBatch(insResourceList);   // 批量插入
            // 批量更新
            cp = new HashSet<>(originResourceSet);
            cp.retainAll(resourceViewSet);
            for(String t : cp) {
                HzResourceView view = resourceViewMap.get(t);
                Resourcegroup resourcegroup = resourcegroupMap.get(t);
                Resource resource = new Resource();
                resource.setResourceType(Integer.parseInt(view.getDeviceType()));
                resource.setResourceGroupIdIsF(resourcegroup.getResourceGroupId());
                resource.setResourceGroupId(resourcegroup.getResourceGroupParentId());
                resource.setDevicename(view.getDeviceName());
                resource.setDevicestatus(view.getDeviceStatus());
                resource.setMouldDrawNo(view.getMouldDrawNo());  resource.setFixedassetid(view.getFixedAssetId());
                resource.setExfactorydate(view.getExfactoryDate());  resource.setRemark(view.getRemark());
                resource.setDevicestatus(view.getDeviceStatus());
                resource.setCavityNumber(view.getCavityNumber());
                resource.setCavitySpecifications(view.getCavitySpecifications());
                resource.setDesignLife(view.getDesignLife());
                resource.setStorageLocation(view.getStorageLocation());
//                resource.setUsedNumber(view.getUsedNumber());


                resource.setFactoryMouldDrawingNumber(view.getFactoryMouldDrawingNumber());
                resource.setFactoryMouldDrawingVersion(view.getFactoryMouldDrawingVersion());
                resource.setCustomMouldDrawingNumber(view.getCustomMouldDrawingNumber());
                resource.setMouldType(view.getMouldType());
                resource.setWeight(view.getWeight());
                resource.setNameplateNumber(view.getNameplateNumber());
                resource.setOwnerName(view.getOwnerName());
                resource.setAssetFactory(view.getAssetFactory());
                resource.setLocationNumber(view.getLocationNumber());
                resource.setLocationName(view.getLocationName());
                resource.setModelStorageLocation(view.getModelStorageLocation());
                resource.setModelAdditionalInformation(view.getModelAdditionalInformation());
                resource.setMouldOpenOrderDay(view.getMouldOpenOrderDay());
                resource.setMouldOpenDay(view.getMouldOpenDay());
                resource.setExpectedCompletionDateOfMouldOpen(view.getExpectedCompletionDateOfMouldOpen());
                resource.setWarrantyExpirationDate(view.getWarrantyExpirationDate());
                resource.setInjectionMachineOunces(view.getInjectionMachineOunces());
                resource.setMachinePipeDiameter(view.getMachinePipeDiameter());
                resource.setSafeDesignLife(view.getSafeDesignLife());

                resource.setScrapDate(view.getScrapDate());
                resource.setScrapReason(view.getScrapReason());
                resource.setDeactivationDate(view.getDeactivationDate());
                resource.setDeactivationReason(view.getDeactivationReason());
                resource.setReactivationDay(view.getReactivationDay());
                resource.setInvMaterial(view.getInvMaterial());

                resource.setSpecification(view.getSpecification());  // 规格(模具规格)
                resourceMapper.update(resource, new LambdaQueryWrapper<Resource>().eq(Resource::getDeviceid, t));
            }
            if(resourceLog == null) {
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclog.setTablename("resource");
                tablesynclogService.save(tablesynclog);
            }else{
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclogService.update(tablesynclog, new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "resource"));
            }
        }
        System.out.println("resourceGroup_ResourceViewSync end.........");
//        // 更新PPR
//        List<HzResourceGroupView> hzPPRViewList = hzViewSyncMapper.hzPPRViewList();
//        List<Productprocessresource> pprList = productprocessresourceService.list();
//        for(HzResourceGroupView t : hzPPRViewList) {
//
//        }
    }

    @Scheduled(cron = "0 0/40 * * * ?")
    @DSTransactional
    public void coptdViewSync() {
        System.out.println("active = " + active);
        if(!"hz".equals(active)) {
            return;
        }
        System.out.println("COPTDSync....." + TimeUtils.getFullNowTime());
        Tablesynclog coptdLog = tablesynclogService.getOne(new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "COPTD"));
        List<HzCoptdView> viewList = null;
        if(coptdLog == null || StringUtils.isEmpty(coptdLog.getLastsynctime())) {   // 如果没记录，则是第一次同步
            viewList = hzViewSyncMapper.hzCoptdViewList(null);
        }else{
            viewList = hzViewSyncMapper.hzCoptdViewList(coptdLog.getLastsynctime());
        }
        if(viewList != null && viewList.size() > 0) {
            List<String> coptdKeyList = coptdMapper.getCoptdKeyList();
            Set<String> originSet = new HashSet<>();
            for(String t : coptdKeyList) originSet.add(t);

            Set<String> viewSet = new HashSet<>();
            Map<String, HzCoptdView> viewMap = new HashMap<>();
            String lastTime = "";
            for(int i = 0; i < viewList.size(); i++) {
                viewList.set(i, LanguageConversionUtils.convertToSimplifiedChinese(viewList.get(i)));
            }
            for(HzCoptdView item : viewList) {
                viewMap.put(item.getTD001().trim() + "^" + item.getTD002().trim() + "^" + item.getTD003().trim(), item);
                viewSet.add(item.getTD001().trim() + "^" + item.getTD002().trim() + "^" + item.getTD003().trim());
                if(lastTime == "") lastTime = item.getModified_time();
                else {
                    if(lastTime.compareTo(item.getModified_time()) < 0) {
                        lastTime = item.getModified_time();
                    }
                }
            }
            Set<String> cp = new HashSet<>(viewSet);
            cp.removeAll(originSet);
            List<Coptd> insCoptdList = new ArrayList<>();
            for(String t : cp) {  // 插入VIEW中多的
                HzCoptdView view = viewMap.get(t);
                Coptd coptd = new Coptd();
                BeanUtils.copyPropertiesIgnoreCase(view, coptd);
                insCoptdList.add(coptd);
            }
            // 批量插入
            coptdService.saveBatch(insCoptdList);
            // 批量更新
            cp = new HashSet<>(originSet);
            cp.retainAll(viewSet);
            for(String t : cp) {
                HzCoptdView view = viewMap.get(t);
                Coptd coptd = new Coptd();
                BeanUtils.copyPropertiesIgnoreCase(view, coptd);
                coptdMapper.update(coptd, new LambdaQueryWrapper<Coptd>().eq(Coptd::getTd001, view.getTD001())
                        .eq(Coptd::getTd002, view.getTD002()).eq(Coptd::getTd003, view.getTD003()));
            }
            List<String> keyView = hzViewSyncMapper.hzCoptdViewListKeyList();
            Set<String> viewAllSet = new HashSet<>();
            for(String t : keyView) viewAllSet.add(t);
            // 查看有没有VIEW中没有的，如果没有后续单据，需要删除。有的话，则禁用
            cp = new HashSet<>(originSet);
            cp.removeAll(viewAllSet);
            for(String t : cp) {  // 删除我们这边有的，VIEW中没有的
                String[] split = t.split("\\^");
                coptdMapper.delete(new LambdaQueryWrapper<Coptd>().eq(Coptd::getTd001, split[0])
                        .eq(Coptd::getTd002, split[1]).eq(Coptd::getTd003, split[2]));
            }
            if(coptdLog == null) {
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclog.setTablename("COPTD");
                tablesynclogService.save(tablesynclog);
            }else{
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclogService.update(tablesynclog, new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "COPTD"));
            }
        }
        System.out.println("COPTD end....." + TimeUtils.getFullNowTime());
    }

    @Scheduled(cron = "0 0/30 * * * ?")
    @DSTransactional
    public void purcdViewSync() {
        System.out.println("active = " + active);
        if(!"hz".equals(active)) {
            return;
        }
        System.out.println("purcdSync....." + TimeUtils.getFullNowTime());
        Tablesynclog purcdLog = tablesynclogService.getOne(new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "PURCD"));
        List<HzPurcdView> viewList = null;
        if(purcdLog == null || StringUtils.isEmpty(purcdLog.getLastsynctime())) {   // 如果没记录，则是第一次同步
            viewList = hzViewSyncMapper.hzPurcdViewList(null);
        }else{
            viewList = hzViewSyncMapper.hzPurcdViewList(purcdLog.getLastsynctime());
        }
        if(viewList != null && viewList.size() > 0) {
            List<String> purcdKeyList = purcdMapper.getPurcdKeyList();
            Set<String> originSet = new HashSet<>();
            for(String t : purcdKeyList) originSet.add(t);

            Set<String> viewSet = new HashSet<>();
            Map<String, HzPurcdView> viewMap = new HashMap<>();
            String lastTime = "";
            for(int i = 0; i < viewList.size(); i++) {
                viewList.set(i, LanguageConversionUtils.convertToSimplifiedChinese(viewList.get(i)));
            }
            for(HzPurcdView item : viewList) {
                viewMap.put(item.getCD001().trim() + "^" + item.getCD002().trim() + "^" + item.getCD003().trim(), item);
                viewSet.add(item.getCD001().trim() + "^" + item.getCD002().trim() + "^" + item.getCD003().trim());
                if(lastTime == "") lastTime = item.getModified_time();
                else {
                    if(lastTime.compareTo(item.getModified_time()) < 0) {
                        lastTime = item.getModified_time();
                    }
                }
            }
            Set<String> cp = new HashSet<>(viewSet);
            cp.removeAll(originSet);
            List<Purcd> insPurcdList = new ArrayList<>();
            for(String t : cp) {  // 插入VIEW中多的
                HzPurcdView view = viewMap.get(t);
                Purcd purcd = new Purcd();
                BeanUtils.copyPropertiesIgnoreCase(view, purcd);
                insPurcdList.add(purcd);
            }
            // 批量插入
            purcdService.saveBatch(insPurcdList);
            // 批量更新
            cp = new HashSet<>(originSet);
            cp.retainAll(viewSet);
            for(String t : cp) {
                HzPurcdView view = viewMap.get(t);
                Purcd purcd = new Purcd();
                BeanUtils.copyPropertiesIgnoreCase(view, purcd);
                purcdMapper.update(purcd, new LambdaQueryWrapper<Purcd>().eq(Purcd::getCd001, view.getCD001())
                        .eq(Purcd::getCd002, view.getCD002()).eq(Purcd::getCd003, view.getCD003()));
            }
            List<String> keyView = hzViewSyncMapper.hzPurcdViewListKeyList();
            Set<String> viewAllSet = new HashSet<>();
            for(String t : keyView) viewAllSet.add(t);
            // 查看有没有VIEW中没有的，如果没有后续单据，需要删除。有的话，则禁用
            cp = new HashSet<>(originSet);
            cp.removeAll(viewAllSet);
            for(String t : cp) {  // 删除我们这边有的，VIEW中没有的
                String[] split = t.split("\\^");
                purcdMapper.delete(new LambdaQueryWrapper<Purcd>().eq(Purcd::getCd001, split[0])
                        .eq(Purcd::getCd002, split[1]).eq(Purcd::getCd003, split[2]));
            }
            if(purcdLog == null) {
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclog.setTablename("PURCD");
                tablesynclogService.save(tablesynclog);
            }else{
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclogService.update(tablesynclog, new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "PURCD"));
            }
        }
        System.out.println("PURCD end....." + TimeUtils.getFullNowTime());
    }

    @Scheduled(cron = "0 0/30 * * * ?")
    @DSTransactional
    public void purtdViewSync() {
        System.out.println("active = " + active);
        if(!"hz".equals(active)) {
            return;
        }
        System.out.println("PURTDSync....." + TimeUtils.getFullNowTime());
        Tablesynclog purtdLog = tablesynclogService.getOne(new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "PURTD"));
        List<HzPurtdView> viewList = null;
        if(purtdLog == null || StringUtils.isEmpty(purtdLog.getLastsynctime())) {   // 如果没记录，则是第一次同步
            viewList = hzViewSyncMapper.hzPurtdViewList(null);
        }else{
            viewList = hzViewSyncMapper.hzPurtdViewList(purtdLog.getLastsynctime());
        }
        if(viewList != null && viewList.size() > 0) {
            List<String> purtdKeyList = purtdMapper.getPurtdKeyList();
            Set<String> originSet = new HashSet<>();
            for(String t : purtdKeyList) originSet.add(t);

            Set<String> viewSet = new HashSet<>();
            Map<String, HzPurtdView> viewMap = new HashMap<>();
            String lastTime = "";
            for(int i = 0; i < viewList.size(); i++) {
                viewList.set(i, LanguageConversionUtils.convertToSimplifiedChinese(viewList.get(i)));
            }
            for(HzPurtdView item : viewList) {
                viewMap.put(item.getTD001().trim() + "^" + item.getTD002().trim() + "^" + item.getTD003().trim(), item);
                viewSet.add(item.getTD001().trim() + "^" + item.getTD002().trim() + "^" + item.getTD003().trim());
                if(lastTime == "") lastTime = item.getModified_time();
                else {
                    if(lastTime.compareTo(item.getModified_time()) < 0) {
                        lastTime = item.getModified_time();
                    }
                }
            }
            Set<String> cp = new HashSet<>(viewSet);
            cp.removeAll(originSet);
            List<Purtd> insPurtdList = new ArrayList<>();
            for(String t : cp) {  // 插入VIEW中多的
                HzPurtdView view = viewMap.get(t);
                Purtd purtd = new Purtd();
                BeanUtils.copyPropertiesIgnoreCase(view, purtd);
                insPurtdList.add(purtd);
            }
            // 批量插入
            purtdService.saveBatch(insPurtdList);
            // 批量更新
            cp = new HashSet<>(originSet);
            cp.retainAll(viewSet);
            for(String t : cp) {
                HzPurtdView view = viewMap.get(t);
                Purtd purtd = new Purtd();
                BeanUtils.copyPropertiesIgnoreCase(view, purtd);
                purtdMapper.update(purtd, new LambdaQueryWrapper<Purtd>().eq(Purtd::getTd001, view.getTD001())
                        .eq(Purtd::getTd002, view.getTD002()).eq(Purtd::getTd003, view.getTD003()));
            }
            List<String> keyView = hzViewSyncMapper.hzPurtdViewListKeyList();
            Set<String> viewAllSet = new HashSet<>();
            for(String t : keyView) viewAllSet.add(t);
            // 查看有没有VIEW中没有的，如果没有后续单据，需要删除。有的话，则禁用
            cp = new HashSet<>(originSet);
            cp.removeAll(viewAllSet);
            for(String t : cp) {  // 删除我们这边有的，VIEW中没有的
                String[] split = t.split("\\^");
                purtdMapper.delete(new LambdaQueryWrapper<Purtd>().eq(Purtd::getTd001, split[0])
                        .eq(Purtd::getTd002, split[1]).eq(Purtd::getTd003, split[2]));
            }
            if(purtdLog == null) {
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclog.setTablename("PURTD");
                tablesynclogService.save(tablesynclog);
            }else{
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclogService.update(tablesynclog, new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "PURTD"));
            }
        }
        System.out.println("PURTD end....." + TimeUtils.getFullNowTime());
    }



//    @Scheduled(cron = "0 0/13 * * * ?")
//    @DSTransactional
//    public void syncMOCTAMERGE() {
//    }

//    @Scheduled(cron = "0 0/23 * * * ?")
//    @DSTransactional
//    public void moctbViewSync() {
//        System.out.println("active = " + active);
//        if(!"hz".equals(active)) {
//            return;
//        }
//        System.out.println("MOCTBSync....." + TimeUtils.getFullNowTime());
//        Tablesynclog moctbLog = tablesynclogService.getOne(new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "MOCTB"));
//        List<HzMoctbView> moctbViewList = null;
//        if(moctbLog == null || StringUtils.isEmpty(moctbLog.getLastsynctime())) {   // 如果没记录，则是第一次同步
//            moctbViewList = hzViewSyncMapper.hzMoctbViewList(null);
//        }else{
//            moctbViewList = hzViewSyncMapper.hzMoctbViewList(moctbLog.getLastsynctime());
//        }
//        if(moctbViewList != null && moctbViewList.size() > 0) {
//            System.out.println("MOCTB 取数据" + TimeUtils.getFullNowTime());
//            List<String> moctbKeyList = moctbMapper.getmoctbKeyList();
//            Set<String> originSet = new HashSet<>();
//            for(String t : moctbKeyList) originSet.add(t);
//
//            Set<String> viewSet = new HashSet<>();
//            Map<String, HzMoctbView> viewMap = new HashMap<>();
//            String lastTime = "";
//            for(int i = 0; i < moctbViewList.size(); i++) {
//                moctbViewList.set(i, LanguageConversionUtils.convertToSimplifiedChinese(moctbViewList.get(i)));
//            }
//            for(HzMoctbView item : moctbViewList) {
//                if(StringUtils.isEmpty(item.getTB001()) || StringUtils.isEmpty(item.getTB002()) || item.getLINE_NO() == null) continue;
//                viewMap.put(item.getTB001().trim() + "^" + item.getTB002().trim() + "^" + item.getLINE_NO().toString().trim(), item);
//                viewSet.add(item.getTB001().trim() + "^" + item.getTB002().trim() + "^" + item.getLINE_NO().toString().trim());
//                if(lastTime == "") lastTime = item.getModified_time();
//                else {
//                    if(lastTime.compareTo(item.getModified_time()) < 0) {
//                        lastTime = item.getModified_time();
//                    }
//                }
//            }
//            System.out.println("MOCTB 新增数据" + TimeUtils.getFullNowTime());
//            Set<String> cp = new HashSet<>(viewSet);
//            cp.removeAll(originSet);
//            List<Moctb2> insMoctbList = new ArrayList<>();
////            Map<String, Moctb> insMoctbMergeMap = new HashMap<>();
//            for(String t : cp) {  // 插入VIEW中多的
//                HzMoctbView view = viewMap.get(t);
//                if(view.getMATTYPE() == null) view.setMATTYPE("");
//                if(view.getBommat_id() == null) view.setBommat_id("");
//                Moctb2 moctb = new Moctb2();
//                BeanUtils.copyPropertiesIgnoreCase(view, moctb);
//                moctb.setUdf03(view.getLINE_NO().toString());
//                moctb.setUdf04(view.getMATTYPE());
//                moctb.setUdf05(view.getBommat_id());
//                moctb.setUdf07(view.getHasAltInv());  //
//                moctb.setUdf08(view.getAlter_Rate()); //
//                moctb.setCreator("ERP");
//                moctb.setCreateDate(TimeUtils.getNowTime());
//
//                StringBuffer sb = new StringBuffer();
//                sb.append(moctb.getTb001().trim()).append(view.getTA025TA006().trim()).append(view.getTB006().trim())
//                        .append(view.getTB003().trim());
//                sb.append(view.getMATTYPE().trim());
//                sb.append(view.getBommat_id().trim());
//
////                Moctb x = insMoctbMergeMap.get(sb.toString());
//                Moctb x = moctbMapper.selX(moctb.getTb001(), view.getTA025TA006(), view.getTB006(), view.getTB003(),
//                        view.getMATTYPE(), view.getBommat_id());
//                Boolean f = false;
//                if(x == null) {
//                    f = true;
//                    x = new Moctb();
//                    BeanUtils.copyPropertiesIgnoreCase(moctb, x);
//                    x.setTb002(view.getTA025TA006());
//                }
//                if(!f) {
//                    x.setTb004(view.getTB004() + x.getTb004());
//                    x.setTb005(view.getTB005() + x.getTb005());
//                    moctbService.update(x, new LambdaQueryWrapper<Moctb>()
//                            .eq(Moctb::getTb001, x.getTb001())
//                            .eq(Moctb::getTb002, x.getTb002())
//                            .eq(Moctb::getTb003, x.getTb003())
//                            .eq(Moctb::getUdf04, x.getUdf04())
//                            .eq(Moctb::getUdf05, x.getUdf05()));
//                }else{
//                    moctbService.save(x);
//                }
////                insMoctbMergeMap.put(sb.toString(), x);
//
//                insMoctbList.add(moctb);
//            }
//            // 批量插入
//            moctb2Service.saveBatch(insMoctbList);
////            List<Moctb> insMoctb = new ArrayList<>();
////            for (Map.Entry<String, Moctb> entry : insMoctbMergeMap.entrySet()) {
////                System.out.println("Key: " + entry.getKey() + ", Value: " +);
////                insMoctb.add(entry.getValue());
////            }
////            moctbService.saveBatch(insMoctb);
//            System.out.println("MOCTB 更新数据" + TimeUtils.getFullNowTime());
//            // 批量更新
//            cp = new HashSet<>(viewSet);
//            cp.retainAll(originSet);
//
//            for(String t : cp) {
//                HzMoctbView view = viewMap.get(t);
//                if(view.getMATTYPE() == null) view.setMATTYPE("");
//                if(view.getBommat_id() == null) view.setBommat_id("");
//                Moctb2 moctb = new Moctb2();
//                BeanUtils.copyPropertiesIgnoreCase(view, moctb);
//                moctb.setUdf03(view.getLINE_NO().toString());
//                moctb.setUdf04(view.getMATTYPE());
//                moctb.setUdf05(view.getBommat_id());
//                moctb.setUdf07(view.getHasAltInv());  //
//                moctb.setUdf08(view.getAlter_Rate()); //
//                moctb.setModifier("ERP"); moctb.setModiDate(TimeUtils.getNowTime());
//
//                Moctb2 oldMoctb2 = moctb2Mapper.selectOne(new LambdaQueryWrapper<Moctb2>()
//                        .eq(Moctb2::getTb001, view.getTB001())
//                        .eq(Moctb2::getTb002, view.getTB002())
//                        .eq(Moctb2::getUdf03, view.getLINE_NO().toString().trim()));
//                Moctb x = moctbMapper.selX(moctb.getTb001(), view.getTA025TA006(), view.getTB006(), view.getTB003(),
//                        view.getMATTYPE(), view.getBommat_id());
//                x.setTb004(view.getTB004() + x.getTb004() - oldMoctb2.getTb004());
//                x.setTb005(view.getTB005() + x.getTb005() - oldMoctb2.getTb005());
//                moctbService.update(x, new LambdaQueryWrapper<Moctb>()
//                        .eq(Moctb::getTb001, x.getTb001())
//                        .eq(Moctb::getTb002, x.getTb002())
//                        .eq(Moctb::getTb003, x.getTb003())
//                        .eq(Moctb::getUdf04, x.getUdf04())
//                        .eq(Moctb::getUdf05, x.getUdf05()));
//
//                moctb2Service.update(moctb, new LambdaQueryWrapper<Moctb2>()
//                        .eq(Moctb2::getTb001, view.getTB001())
//                        .eq(Moctb2::getTb002, view.getTB002())
//                        .eq(Moctb2::getUdf03, view.getLINE_NO().toString().trim()));
//
//            }
//
//            if(moctbLog == null) {
//                Tablesynclog tablesynclog = new Tablesynclog();
//                tablesynclog.setLastsynctime(lastTime);
//                tablesynclog.setTablename("MOCTB");
//                tablesynclogService.save(tablesynclog);
//            }else{
//                Tablesynclog tablesynclog = new Tablesynclog();
//                tablesynclog.setLastsynctime(lastTime);
//                tablesynclogService.update(tablesynclog, new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "MOCTB"));
//            }
//
//        }
//        System.out.println("MOCTB end....." + TimeUtils.getFullNowTime());
//    }

    @Scheduled(cron = "0 0/25 * * * ?")
    @DSTransactional
    public void productprocessresourceServiceViewSync() {
        System.out.println("active = " + active);
        if(!"hz".equals(active)) {
            return;
        }
        System.out.println("PPR ViewSync start.........");
        /**
         * resource  VIEW中只有模具的内容
         */
        List<HzPPRView> hzPPRViewList = null;
        Tablesynclog hzPPRLog = tablesynclogService.getOne(new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "productprocessresource"));
        if(hzPPRLog == null || StringUtils.isEmpty(hzPPRLog.getLastsynctime())) {   // 如果没记录，则是第一次同步
            hzPPRViewList = hzViewSyncMapper.hzPPRViewList(null);
        }else{
            hzPPRViewList = hzViewSyncMapper.hzPPRViewList(hzPPRLog.getLastsynctime());
        }
        if(hzPPRViewList != null && hzPPRViewList.size() > 0) {
            String lastTime = "";
            for(int i = 0; i < hzPPRViewList.size(); i++) {
                hzPPRViewList.set(i, LanguageConversionUtils.convertToSimplifiedChinese(hzPPRViewList.get(i)));
            }
            Set<String> hzPPRViewSet = new HashSet<>();
            Map<String, HzPPRView> hzPPRViewMap = new HashMap<>();

            for(HzPPRView item : hzPPRViewList) {
                if(lastTime == "") lastTime = item.getModified_Time();
                else {
                    if(lastTime.compareTo(item.getModified_Time()) < 0) {
                        lastTime = item.getModified_Time();
                    }
                }
                StringBuffer sb = new StringBuffer(); sb.append(item.getInv_no().trim()).append(item.getProductId().trim()).append(item.getProcessRoute().trim());
                sb.append(item.getInvrt_name().trim()).append(item.getProcessId().trim()).append(item.getProcessno().trim()).append(item.getResourceTypeId().toString().trim());
                sb.append(item.getResourceGroupNO().trim()).append(item.getMachiningCenter().trim());
                hzPPRViewSet.add(sb.toString());
                hzPPRViewMap.put(sb.toString(), item);
            }

            Set<String> productIdSet = new HashSet<>();

//            List<Resourcegroup> resourcegroupList = resourcegroupMapper.selectList(new LambdaQueryWrapper<Resourcegroup>().eq(Resourcegroup::getType, "M"));
            List<Resourcegroup> resourcegroupList = resourcegroupMapper.selectList(null);
            Map<String, Integer> resourcegroupMap = new HashMap<>();
            for(Resourcegroup t : resourcegroupList) resourcegroupMap.put(t.getResourceGroupNO().trim(), t.getResourceGroupId());

            List<Productprocessresource> resourceList = productprocessresourceMapper.selList();
            Set<String> originPPRSet = new HashSet<>();
            Map<String, Productprocessresource> originPPRMap = new HashMap<>();
            for(Productprocessresource item : resourceList) {
                StringBuffer sb = new StringBuffer();  sb.append(item.getInv_no().trim()).append(item.getProductId().trim()).append(item.getProcessRoute().trim());
                sb.append(item.getInvrt_name().trim()).append(item.getProcessId().trim()).append(item.getProcessNo().trim()).append(item.getResourceTypeId().toString().trim());
                sb.append(item.getResourceGroupNO().trim()).append(item.getMachiningCenter().trim());
                originPPRSet.add(sb.toString());
                originPPRMap.put(sb.toString(), item);
            }
            Set<String> cp = new HashSet<>(hzPPRViewSet);
            cp.removeAll(originPPRSet);
            List<Productprocessresource> insPPRList = new ArrayList<>();
            for(String t : cp) {
                HzPPRView view = hzPPRViewMap.get(t);
                productIdSet.add(view.getProductId().trim());
                Productprocessresource productprocessresource = new Productprocessresource();
                BeanUtils.copyPropertiesIgnoreCase(view, productprocessresource);
                productprocessresource.setResourceGroupId(resourcegroupMap.get(view.getResourceGroupNO().trim()));
                insPPRList.add(productprocessresource);
            }
            productprocessresourceService.saveBatch(insPPRList);   // 批量插入
            // 批量更新
            cp = new HashSet<>(originPPRSet);
            cp.retainAll(hzPPRViewSet);
            for(String t : cp) {
                HzPPRView view = hzPPRViewMap.get(t);
                productIdSet.add(view.getProductId().trim());
                Productprocessresource productprocessresource = originPPRMap.get(t);
                Integer originResourceGroupId = productprocessresource.getResourceGroupId();
                BeanUtils.copyPropertiesIgnoreCase(view, productprocessresource);
                productprocessresource.setResourceGroupId(resourcegroupMap.get(view.getResourceGroupNO().trim()));
                productprocessresourceMapper.update(productprocessresource,
                        new LambdaQueryWrapper<Productprocessresource>()
                                .eq(Productprocessresource::getInv_no, view.getInv_no().trim())
                                .eq(Productprocessresource::getProductId, view.getProductId().trim())
                                .eq(Productprocessresource::getProcessRoute, view.getProcessRoute().trim())
                                .eq(Productprocessresource::getInvrt_name, view.getInvrt_name().trim())
                                .eq(Productprocessresource::getProcessId, view.getProcessId().trim())
                                .eq(Productprocessresource::getProcessNo, view.getProcessno().trim())
                                .eq(Productprocessresource::getResourceTypeId, view.getResourceTypeId().toString().trim())
                                .eq(Productprocessresource::getResourceGroupId, originResourceGroupId)
                                .eq(Productprocessresource::getMachiningCenter, view.getMachiningCenter().trim()));
            }
            // 现在VIEW中全部的PPR
            List<HzPPRView> allHzPPRViewList = hzViewSyncMapper.hzPPRViewList(null);
            // PPR的删除
            for(int i = 0; i < allHzPPRViewList.size(); i++) {
                allHzPPRViewList.set(i, LanguageConversionUtils.convertToSimplifiedChinese(allHzPPRViewList.get(i)));
            }
            Set<String> allHzPPRViewSet = new HashSet<>();
            for(HzPPRView item : allHzPPRViewList) {
                StringBuffer sb = new StringBuffer(); sb.append(item.getInv_no().trim()).append(item.getProductId().trim()).append(item.getProcessRoute().trim());
                sb.append(item.getInvrt_name().trim()).append(item.getProcessId().trim()).append(item.getProcessno().trim()).append(item.getResourceTypeId().toString().trim());
                sb.append(item.getResourceGroupNO().trim()).append(item.getMachiningCenter().trim());
                allHzPPRViewSet.add(sb.toString());
            }
            cp = new HashSet<>(originPPRSet);
            cp.removeAll(allHzPPRViewSet);
            List<Integer> needToDelList = new ArrayList<>();
            List<Viewsynclog> viewsynclogList = new ArrayList<>();
            for(String t : cp) {
                Productprocessresource productprocessresource = originPPRMap.get(t);
                productIdSet.add(productprocessresource.getProductId().trim());
                needToDelList.add(productprocessresource.getId());
                // 同步日志记录
                Viewsynclog viewsynclog = new Viewsynclog("PPR", "DELETE", "删除内容:" + productprocessresource.toString(),
                        TimeUtils.getFullNowTime());
                viewsynclogList.add(viewsynclog);
            }
            viewsynclogService.saveBatch(viewsynclogList);
//            System.out.println(needToDelList);
//            productprocessresourceMapper.deleteBatchIds(needToDelList);
            productprocessresourceService.removeByIds(needToDelList);

            List<String> productIdList = new ArrayList<>();
            for(String s : productIdSet) productIdList.add(s);
            // TODO 去更新所有待排机 && 工单还未结束的
            if(productIdList.size() == 0) productIdList.add("xxxxxxxxxxxxxxxxx");
//            List<MOCTA> moctaList = scheduleMapper.getNoStartMocta(productIdList);
//            if(moctaList != null && moctaList.size() > 0) {
//                for(MOCTA mocta : moctaList) {
//                    // 0.删除所有的MOCTY 和 workresource
//                    moctyMapper.delete(new LambdaQueryWrapper<Mocty>().eq(Mocty::getTy001, mocta.getTA001()).eq(Mocty::getTy002, mocta.getTA002()));
//                    workresourceService.remove(new LambdaQueryWrapper<Workresource>().eq(Workresource::getWorkSingle, mocta.getTA001()).eq(Workresource::getWorkNo, mocta.getTA002()));
//                    // 找出所有的派工单
//
//                    // 1.补好派工单
//                    String uuid = UUID.randomUUID().toString();
//                    int productNumPerDay = 0;
//                    // 如果有产能的，需要算机器每日的产能。   如果没有的话，则直接排满
//                    List<SFCTA> sfctaList = sfctaMapper.selectList(new LambdaQueryWrapper<SFCTA>()
//                            .eq(SFCTA::getTA001, view.getTA001().trim()).eq(SFCTA::getTA002, view.getTA025TA006().trim()).orderByAsc(SFCTA::getTA003));
//                    if(sfctaList == null || sfctaList.size() == 0) {
//                        log.info(view.getTA001().trim() + " , " + view.getTA025TA006() + " no sfcta data...");
//                        continue;
//                    }
//                    SFCTA sfcta = sfctaList.get(0);
//                    List<Productprocessresource> list = productprocessresourceMapper.selPPRListHzVersion(
//                            new GetProductLineMachineHzViewDto(mocta.getTA006(), sfcta.getTA003(), sfcta.getTA004(), mocta.getUDF01(),
//                                    mocta.getUDF02(), mocta.getTA021(), mocta.getUDF04(), null, 1));
//                    boolean flag = false;
//                    Integer jutiId = null;
//                    for(Productprocessresource productprocessresource : list) {
//                        if("ST4".equals(mocta.getTA021().trim()) || "ST6".equals(mocta.getTA021().trim())) {
//                            // 如果是装配车间的话，不需要填充 1和2 之外的东西
//                            if(!productprocessresource.getResourceTypeId().equals(1) && !productprocessresource.getResourceTypeId().equals(2)) {
//                                continue;
//                            }
//                        }
//                        if(productprocessresource.getResourceTypeId().equals(1) || productprocessresource.getResourceTypeId().equals(2)) {   // 看一下这个工单能不能选到具体的 生产线/设备
//                            List<Integer> idList = resourceMapper.getResourceIdByTY(productprocessresource.getResourceGroupId(), mocta.getTA021());
//                            if(idList == null || idList.size() == 0) continue;
//                            if(idList != null && idList.size() == 1) {  // 如果末级资源过滤之后，只剩下一个。则直接添加 (直接通过工作中心，过滤)
//                                jutiId = idList.get(0);
//                            }
//                            BigDecimal hours = hzCmsmxMap.get(productprocessresource.getResourceGroupNO().trim());
//                            double v = hours.doubleValue();
//                            productNumPerDay = (int) Math.floor(v * 3600 / productprocessresource.getBeat());  // 计算日产量
//                            flag = true;  break;
//                        }
//                    }
//                    if(!flag) {  // 如果没有绑定生产线/设备
//
//                        Mocty mocty = new Mocty();
//                        mocty.setCreator("ERP"); mocty.setCreateDate(TimeUtils.getNowTime());
//                        mocty.setTy001(view.getTA001().trim());
//                        mocty.setTy002(view.getTA025TA006().trim());
//                        mocty.setTy003(view.getTA009().trim());
//                        mocty.setTy004(view.getTA015().doubleValue());
//                        mocty.setTy009("****");  mocty.setTy019(sfcta.getTA003());  // 工序
//                        mocty.setUdf05(UUID.randomUUID().toString());
//                        mocty.setUdf06(uuid);
//                        mocty.setUdf11(view.getTA001().trim());  // 原MOCTA TA001
//                        mocty.setUdf12(view.getTA002().trim());  // 原MOCTA TA002
//                        mocty.setUdf01(MOCStatusEnum.DaiPaiJi.getValue());
//                        mocty.setUdf04("转入参数: " + view.toString());
//                        insMoctyList.add(mocty);
//                        for(Productprocessresource productprocessresource : list) {
//                            if("ST4".equals(mocta.getTA021().trim()) || "ST6".equals(mocta.getTA021().trim())) {
//                                // 如果是装配车间的话，不需要填充 1和2 之外的东西
//                                if(!productprocessresource.getResourceTypeId().equals(1) && !productprocessresource.getResourceTypeId().equals(2)) {
//                                    continue;
//                                }
//                            }
//                            Workresource workresource = new Workresource();
//                            workresource.setWorkResourceId(UUID.randomUUID().toString());
//                            workresource.setWorkSingle(mocta.getTA001());
//                            workresource.setWorkNo(mocta.getTA025().trim() + "-" + mocta.getTA006().trim());
//                            workresource.setProcessNo(sfcta.getTA003());
//                            workresource.setProcessId(sfcta.getTA004());
//                            workresource.setResourceGroupId(productprocessresource.getResourceGroupId());
//                            if(productprocessresource.getResourceTypeId().equals(1) || productprocessresource.getResourceTypeId().equals(2)) {
//                                List<Integer> idList = resourceMapper.getResourceIdByTY(productprocessresource.getResourceGroupId(), mocta.getTA021());
//                                if(idList != null && idList.size() == 1) {  // 如果末级资源过滤之后，只剩下一个。则直接添加 (直接通过工作中心，过滤)
//                                    workresource.setResourceId(idList.get(0));
//                                }
//                            }else{
//                                List<Integer> idList = resourceMapper.getResourceIdByTY(productprocessresource.getResourceGroupId(), null);
//                                if(idList != null && idList.size() == 1) {  // 如果末级资源过滤之后，只剩下一个。则直接添加 (直接通过工作中心，过滤)
//                                    workresource.setResourceId(idList.get(0));
//                                }
//                            }
//                            workresource.setDate(mocta.getTA009());
//                            workresource.setBeat(productprocessresource.getBeat());
//                            workresource.setQuota(productprocessresource.getQuota());
//                            workresource.setCreator("ERP");
//                            workresource.setCreateTime(TimeUtils.getFullNowTime());
//                            workresource.setProductLineId(mocty.getUdf05());   // 和生产线进行绑定
//                            workresource.setPeopleNum(1);
//                            workresource.setResourceTypeId(productprocessresource.getResourceTypeId());
//                            insWorkresourceList.add(workresource);
//                        }
//                    }else{
//                        int day = (int) Math.ceil(mocta.getTA015().doubleValue() / productNumPerDay);
//                        int sum = 0;
//                        // 需要查看当天是否为休息日，如果是的话，则需要往后排
//                        for(int i = 0; i < day; i++) {
//                            Mocty mocty = new Mocty();
//                            mocty.setCreator("ERP"); mocty.setCreateDate(TimeUtils.getNowTime());
//                            mocty.setTy001(view.getTA001().trim());
//                            mocty.setTy002(view.getTA025TA006().trim());
//                            mocty.setTy003(TimeUtils.addOrSubtract(view.getTA009().trim(), i));
//                            if(i == day - 1) mocty.setTy004(mocta.getTA015().doubleValue() - sum);
//                            else {
//                                sum += productNumPerDay;
//                                mocty.setTy004(new Double(productNumPerDay));
//                            }
//                            if(jutiId == null) {
//                                mocty.setTy009("****");
//                                mocty.setUdf01(MOCStatusEnum.DaiPaiJi.getValue());
//                            }
//                            else {
//                                mocty.setTy009(jutiId.toString());
//                                mocty.setUdf01(MOCStatusEnum.DaiPaiJi.getValue());
//                            }
//                            mocty.setTy019(sfcta.getTA003());  // 工序
//                            mocty.setUdf05(UUID.randomUUID().toString());
//                            mocty.setUdf06(uuid);
//                            mocty.setUdf11(view.getTA001().trim());  // 原MOCTA TA001
//                            mocty.setUdf12(view.getTA002().trim());  // 原MOCTA TA002
//                            mocty.setUdf04("转入参数: " + view.toString());
//                            insMoctyList.add(mocty);
//                            for(Productprocessresource productprocessresource : list) {
//                                if("ST4".equals(mocta.getTA021().trim()) || "ST6".equals(mocta.getTA021().trim())) {
//                                    // 如果是装配车间的话，不需要填充 1和2 之外的东西
//                                    if(!productprocessresource.getResourceTypeId().equals(1) && !productprocessresource.getResourceTypeId().equals(2)) {
//                                        continue;
//                                    }
//                                }
//                                Workresource workresource = new Workresource();
//                                workresource.setWorkResourceId(UUID.randomUUID().toString());
//                                workresource.setWorkSingle(mocta.getTA001());
//                                workresource.setWorkNo(mocta.getTA025().trim() + "-" + mocta.getTA006().trim());
//                                workresource.setProcessNo(sfcta.getTA003());
//                                workresource.setProcessId(sfcta.getTA004());
//                                workresource.setResourceGroupId(productprocessresource.getResourceGroupId());
//
//                                if(productprocessresource.getResourceTypeId().equals(1) || productprocessresource.getResourceTypeId().equals(2)) {
//                                    List<Integer> idList = resourceMapper.getResourceIdByTY(productprocessresource.getResourceGroupId(), mocta.getTA021());
//                                    if(idList != null && idList.size() == 1) {  // 如果末级资源过滤之后，只剩下一个。则直接添加 (直接通过工作中心，过滤)
//                                        workresource.setResourceId(idList.get(0));
//                                    }
//                                }else{
//                                    List<Integer> idList = resourceMapper.getResourceIdByTY(productprocessresource.getResourceGroupId(), null);
//                                    if(idList != null && idList.size() == 1) {  // 如果末级资源过滤之后，只剩下一个。则直接添加 (直接通过工作中心，过滤)
//                                        workresource.setResourceId(idList.get(0));
//                                    }
//                                }
//                                workresource.setDate(TimeUtils.addOrSubtract(mocta.getTA009().trim(), i));
//                                workresource.setBeat(productprocessresource.getBeat());
//                                workresource.setQuota(productprocessresource.getQuota());
//                                workresource.setCreator("ERP");
//                                workresource.setCreateTime(TimeUtils.getFullNowTime());
//                                workresource.setProductLineId(mocty.getUdf05());   // 和生产线进行绑定
//                                workresource.setPeopleNum(1);
//                                workresource.setResourceTypeId(productprocessresource.getResourceTypeId());
//                                insWorkresourceList.add(workresource);
//                            }
//                        }
//                    }
//                    // 2.做好大工单
//                }
//            }

            if(hzPPRLog == null) {
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclog.setTablename("productprocessresource");
                tablesynclogService.save(tablesynclog);
            }else{
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclogService.update(tablesynclog, new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "productprocessresource"));
            }
        }
        System.out.println("PPR end.........");
    }

    private static String removeLastDash(String input) {
        return input.substring(0, input.lastIndexOf('-') != -1 ? input.lastIndexOf('-') : input.length());
    }

    @Scheduled(cron = "0 0/25 * * * ?")
    @DSTransactional
    public void mocteViewSync() {
        System.out.println("active = " + active);
        if (!"hz".equals(active)) {
            return;
        }
        System.out.println("mocte ViewSync start.........");
        List<HzMocteView> hzMocteViewList = null;
        Tablesynclog hzMocteLog = tablesynclogService.getOne(new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "MOCTE"));
        if(hzMocteLog == null || StringUtils.isEmpty(hzMocteLog.getLastsynctime())) {   // 如果没记录，则是第一次同步
            hzMocteViewList = hzViewSyncMapper.hzMocteViewList(null);
        }else{
            hzMocteViewList = hzViewSyncMapper.hzMocteViewList(hzMocteLog.getLastsynctime());
        }
        if(hzMocteViewList != null && hzMocteViewList.size() > 0) {

            List<String> mocteKeyList = mocteMapper.getMocteKeyList();
            Set<String> originSet = new HashSet<>();
            for(String t : mocteKeyList) originSet.add(t);

            Set<String> viewSet = new HashSet<>();
            Map<String, HzMocteView> viewMap = new HashMap<>();
            String lastTime = "";
            for(int i = 0; i < hzMocteViewList.size(); i++) {
                hzMocteViewList.set(i, LanguageConversionUtils.convertToSimplifiedChinese(hzMocteViewList.get(i)));
            }
            for(HzMocteView item : hzMocteViewList) {
                viewMap.put(item.getTE001().trim() + "^" + item.getTE002().trim() + "^" + item.getTE003().trim(), item);
                viewSet.add(item.getTE001().trim() + "^" + item.getTE002().trim() + "^" + item.getTE003().trim());
                if(lastTime == "") lastTime = item.getModified_Time();
                else {
                    if(lastTime.compareTo(item.getModified_Time()) < 0) {
                        lastTime = item.getModified_Time();
                    }
                }
            }
            Set<String> cp = new HashSet<>(viewSet);
            cp.removeAll(originSet);

            List<Mocte> insMocteList = new ArrayList<>();
            for(String t : cp) {  // 插入VIEW中多的
                HzMocteView view = viewMap.get(t);
                Mocte mocte = new Mocte();
                BeanUtils.copyPropertiesIgnoreCase(view, mocte);
                insMocteList.add(mocte);
            }
            // 批量插入
            mocteService.saveBatch(insMocteList);
            // 批量更新
            cp = new HashSet<>(originSet);
            cp.retainAll(viewSet);
            for(String t : cp) {
                HzMocteView view = viewMap.get(t);
                Mocte mocte = new Mocte();
                BeanUtils.copyPropertiesIgnoreCase(view, mocte);
                mocteMapper.update(mocte, new LambdaQueryWrapper<Mocte>().eq(Mocte::getTe001, view.getTE001())
                        .eq(Mocte::getTe002, view.getTE002()).eq(Mocte::getTe003, view.getTE003()));
            }
            if(hzMocteLog == null) {
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclog.setTablename("MOCTE");
                tablesynclogService.save(tablesynclog);
            }else{
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclogService.update(tablesynclog, new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "MOCTE"));
            }
        }
        System.out.println("MOCTE end....." + TimeUtils.getFullNowTime());
    }

    @Scheduled(cron = "0 0/25 * * * ?")
    @DSTransactional
    public void moctgViewSync() {
        System.out.println("active = " + active);
        if (!"hz".equals(active)) {
            return;
        }
        System.out.println("moctg ViewSync start.........");
        List<HzMoctgView> hzMoctgViewList = null;
        Tablesynclog hzMoctgLog = tablesynclogService.getOne(new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "MOCTG"));
        if(hzMoctgLog == null || StringUtils.isEmpty(hzMoctgLog.getLastsynctime())) {   // 如果没记录，则是第一次同步
            hzMoctgViewList = hzViewSyncMapper.hzMoctgViewList(null);
        }else{
            hzMoctgViewList = hzViewSyncMapper.hzMoctgViewList(hzMoctgLog.getLastsynctime());
        }
        if(hzMoctgViewList != null && hzMoctgViewList.size() > 0) {

            List<String> moctgKeyList = moctgMapper.getMoctgKeyList();
            Set<String> originSet = new HashSet<>();
            for(String t : moctgKeyList) originSet.add(t);

            Set<String> viewSet = new HashSet<>();
            Map<String, HzMoctgView> viewMap = new HashMap<>();
            String lastTime = "";
            for(int i = 0; i < hzMoctgViewList.size(); i++) {
                hzMoctgViewList.set(i, LanguageConversionUtils.convertToSimplifiedChinese(hzMoctgViewList.get(i)));
            }
            for(HzMoctgView item : hzMoctgViewList) {
                viewMap.put(item.getTG001().trim() + "^" + item.getTG002().trim() + "^" + item.getTG003().trim(), item);
                viewSet.add(item.getTG001().trim() + "^" + item.getTG002().trim() + "^" + item.getTG003().trim());
                if(lastTime == "") lastTime = item.getModified_Time();
                else {
                    if(lastTime.compareTo(item.getModified_Time()) < 0) {
                        lastTime = item.getModified_Time();
                    }
                }
            }
            Set<String> cp = new HashSet<>(viewSet);
            cp.removeAll(originSet);

            List<Moctg> insMoctgList = new ArrayList<>();
            for(String t : cp) {  // 插入VIEW中多的
                HzMoctgView view = viewMap.get(t);
                Moctg moctg = new Moctg();
                BeanUtils.copyPropertiesIgnoreCase(view, moctg);
                insMoctgList.add(moctg);
            }
            // 批量插入
            moctgService.saveBatch(insMoctgList);
            // 批量更新
            cp = new HashSet<>(originSet);
            cp.retainAll(viewSet);
            for(String t : cp) {
                HzMoctgView view = viewMap.get(t);
                Moctg moctg = new Moctg();
                BeanUtils.copyPropertiesIgnoreCase(view, moctg);
                moctgMapper.update(moctg, new LambdaQueryWrapper<Moctg>().eq(Moctg::getTg001, view.getTG001())
                        .eq(Moctg::getTg002, view.getTG002()).eq(Moctg::getTg003, view.getTG003()));
            }
            if(hzMoctgLog == null) {
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclog.setTablename("MOCTG");
                tablesynclogService.save(tablesynclog);
            }else{
                Tablesynclog tablesynclog = new Tablesynclog();
                tablesynclog.setLastsynctime(lastTime);
                tablesynclogService.update(tablesynclog, new LambdaQueryWrapper<Tablesynclog>().eq(Tablesynclog::getTablename, "MOCTG"));
            }
        }
        System.out.println("MOCTG end....." + TimeUtils.getFullNowTime());
    }

    @Scheduled(cron = "0 0/5 * * * ?")
    @DSTransactional
    public void cmsmwViewSync() {
        System.out.println("active = " + active);
        if(!"hz".equals(active)) {
            return;
        }
        System.out.println("cmsmwViewSync....." + TimeUtils.getFullNowTime());
        List<HzCmsmwView> viewList = hzViewSyncMapper.hzCmsmwViewList(null);
        if(viewList != null && viewList.size() > 0) {
            List<String> originList = cmsmwMapper.mw001List();
            Set<String> originSet = new HashSet<>();
            for(String t : originList) originSet.add(t.trim());
            Set<String> viewSet = new HashSet<>();
            Map<String, HzCmsmwView> viewMap = new HashMap<>();
            for(int i = 0; i < viewList.size(); i++) {
                viewList.set(i, LanguageConversionUtils.convertToSimplifiedChinese(viewList.get(i)));
            }
            for(HzCmsmwView item : viewList) {
                viewMap.put(item.getMW001().trim(), item);
                viewSet.add(item.getMW001().trim());
            }
            Set<String> cp = new HashSet<>(viewSet);
            cp.removeAll(originSet);
            List<Cmsmw> insCmsmwList = new ArrayList<>();
            for(String t : cp) {  // 插入VIEW中多的
                HzCmsmwView view = viewMap.get(t);
                Cmsmw cmsmw = new Cmsmw();
                BeanUtils.copyPropertiesIgnoreCase(view, cmsmw);
                insCmsmwList.add(cmsmw);
            }
            // 批量插入
            cmsmwService.saveBatch(insCmsmwList);
            // 批量更新
            cp = new HashSet<>(originSet);
            cp.retainAll(viewSet);
            for(String t : cp) {
                HzCmsmwView view = viewMap.get(t);
                Cmsmw cmsmw = new Cmsmw();
                BeanUtils.copyPropertiesIgnoreCase(view, cmsmw);
                cmsmwMapper.update(cmsmw, new LambdaQueryWrapper<Cmsmw>().eq(Cmsmw::getMw001, cmsmw.getMw001()));
            }
        }
        System.out.println("CMSMW end....." + TimeUtils.getFullNowTime());
    }

}
